Skip to content

Commit

Permalink
build: allow building with recent C compilers (#72)
Browse files Browse the repository at this point in the history
* nimscript: allow building with recent C compilers

Before this commit, nimutils (and therefore Chalk [1]) could not be
built with recent stable releases of Clang and GCC, as some warnings now
default to an error. This affected Clang 15.0.0 (2022-09-06) or later,
and GCC 14.1 (2024-05-07) or later, and:

- Made it harder to build Chalk outside of a container during
  development. Simply writing the options from this commit on the
  command line after `nimble build` was not sufficient, because that
  wouldn't forward the options to the command used to compile e.g.
  this repo's `strcontainer.c` or `switchboard.c`.

- Would prevent certain OS or compiler version bumps for the builder
  image.

For now, simply disable these errors. Some are in md4c.h, and others
will be resolved when we move to libcon4m.

As background, see e.g.:

- The breaking changes section of the release notes for Clang 16, which
  contains [2]:

      The `-Wimplicit-function-declaration` and `-Wimplicit-int`
      warnings now default to an error in C99, C11, and C17. As of C2x,
      support for implicit function declarations and implicit int has
      been removed, and the warning options will have no effect.
      Specifying `-Wimplicit-int` in C89 mode will now issue warnings
      instead of being a noop.

      -Wincompatible-function-pointer-types` now defaults to an error in
      all C language modes.

- The release notes for Clang 15 [3]:

      The `-Wint-conversion` warning diagnostic for implicit int <->
      pointer conversions now defaults to an error in all C language
      modes.

- The porting guide for GCC 14 [4]:

      The initial ISO C standard and its 1999 revision removed support
      for many C language features that were widely known as sources of
      application bugs due to accidental misuse. For backwards
      compatibility, GCC 13 and earlier diagnosed use of these features
      as warnings only. Although these warnings have been enabled by
      default for many releases, experience shows that these warnings
      are easily ignored, resulting in difficult to diagnose bugs. In
      GCC 14, these issues are now reported as errors, and no output
      file is created, providing clearer feedback to programmers that
      something is wrong.

      #### Implicit function declarations (`-Werror=implicit-function-declaration`)

      It is no longer possible to call a function that has not been
      declared. In general, the solution is to include a header file
      with an appropriate function prototype. Note that GCC will perform
      further type checks based on the function prototype, which can
      reveal further type errors that require additional changes.

      #### Using pointers as integers and vice versa (`-Werror=int-conversion`)

      GCC no longer treats integer types and pointer types as equivalent
      in assignments (including implied assignments of function
      arguments and return values), and instead fails the compilation
      with a type error.

      #### Type checking on pointer types (`-Werror=incompatible-pointer-types`)

      GCC no longer allows implicitly casting all pointer types to all
      other pointer types. This behavior is now restricted to the void *
      type and its qualified variations.

- The Gentoo wiki page on Modern C porting [5].

[1] crashappsec/chalk#290
[2] https://releases.llvm.org/16.0.0/tools/clang/docs/ReleaseNotes.html#potentially-breaking-changes
[3] https://releases.llvm.org/15.0.0/tools/clang/docs/ReleaseNotes.html
[4] https://gcc.gnu.org/gcc-14/porting_to.html#warnings-as-errors
[5] https://wiki.gentoo.org/wiki/Modern_C_porting

* build: suppress errors via -Wno-error, not -Wno

Turn these back into warnings, even when the C compiler produces errors
by default, rather than suppressing the message completely.
  • Loading branch information
ee7 authored Aug 2, 2024
1 parent 7210eca commit 1f7f51e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nimutils/nimscript.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ template applyCommonLinkOptions*(staticLink = true, quiet = true) =
switch("path", ".")
switch("d", "useOpenSSL3")
switch("cincludes", getEnv("HOME").joinPath("/.local/c0/include"))
# Disable some errors for Clang 15+ and GCC 14+.
switch("passC", "-Wno-error=int-conversion")
switch("passC", "-Wno-error=implicit-function-declaration")
switch("passC", "-Wno-error=incompatible-pointer-types")

setupTargetArch(quiet)

Expand Down

0 comments on commit 1f7f51e

Please sign in to comment.