From 8af633b1c3944cbd471cb1734e9bf80399281a6a Mon Sep 17 00:00:00 2001 From: Peter Urban Date: Tue, 17 Sep 2024 13:09:32 +0200 Subject: [PATCH] Use CC variable in the Makefile directly --- .../workflows/test-alternative-compilers.yml | 57 +++++++++++-------- Makefile | 24 +++++++- TOOLS.gcc | 9 +-- TOOLS.ini | 21 +++++-- 4 files changed, 73 insertions(+), 38 deletions(-) diff --git a/.github/workflows/test-alternative-compilers.yml b/.github/workflows/test-alternative-compilers.yml index 4f30d1a..f4e2e0b 100644 --- a/.github/workflows/test-alternative-compilers.yml +++ b/.github/workflows/test-alternative-compilers.yml @@ -14,55 +14,66 @@ jobs: include: - name: windows / clang-cl (c++) os: windows-latest - compiler: 'clang-cl /TP' - make_tool: "nmake" + compiler: 'clang-cl' # /TP forces the file to be compiled as C++ + make_command: "nmake CC=$CC" - name: linux / g++ (c++) os: ubuntu-latest - compiler: 'g++ -x c++' - make_tool: "make" + compiler: 'g++' + extra_flags: '-x c++' + make_command: "make CC=$CC EXTRA_FLAGS=$EXTRA_FLAGS" - name: linux / clang (c) os: ubuntu-latest compiler: 'clang' - make_tool: "make" + make_command: "make CC=$CC" - name: linux / clang (c++) os: ubuntu-latest - compiler: 'clang -x c++' - make_tool: "make" + compiler: 'clang' + extra_flags: '-x c++' + make_command: "make CC=$CC EXTRA_FLAGS=$EXTRA_FLAGS" - name: macos / g++ (c++) os: macos-latest - compiler: 'g++ -x c++' - make_tool: "make" + compiler: 'g++' + extra_flags: '-x c++' + make_command: "make CC=$CC EXTRA_FLAGS=$EXTRA_FLAGS" - name: macos / clang (c) os: macos-latest compiler: 'clang' - make_tool: "make" + make_command: "make CC=$CC" - name: macos / clang (c++) os: macos-latest - compiler: 'clang -x c++' - make_tool: "make" + compiler: 'clang' + extra_flags: '-x c++' + make_command: "make CC=$CC EXTRA_FLAGS=$EXTRA_FLAGS" fail-fast: false + env: + CC: ${{ matrix.compiler }} + EXTRA_FLAGS: ${{ matrix.extra_flags }} + steps: - uses: actions/checkout@v4 - uses: ilammy/msvc-dev-cmd@v1 - - name: Check COMPILER variable + - name: Check CC variable shell: bash - env: - COMPILER: ${{ matrix.compiler }} run: | - output=$(${{ matrix.make_tool }} name_compiler) - echo "$output" - if [[ "$output" != *"${{ matrix.compiler }}"* ]]; then - echo "COMPILER '${{ matrix.compiler }}'' variable not used by the make file!" - #exit 1 + compiler=$(${{ matrix.make_command }} name_compiler) + extra_flags=$(${{ matrix.make_command }} name_extra_flags) + echo "compile command: $compiler $extra_flags" + if [[ "$compiler" != *"${{ matrix.compiler }}"* ]]; then + echo "CC '${{ matrix.compiler }}'' variable not used by the make file!" + exit 1 + fi + if [[ "${{ matrix.extra_flags }}" != "" ]]; then + if [[ "$extra_flags" != *"${{ matrix.extra_flags }}"* ]]; then + echo "CC '${{ matrix.extra_flags }}'' variable not used by the make file!" + exit 1 + fi fi - name: Compile shell: bash - env: - COMPILER: ${{ matrix.compiler }} - run: ${{ matrix.make_tool }} + run: ${{ matrix.make_command }} - name: Tests shell: bash diff --git a/Makefile b/Makefile index 0b5b252..fcb0237 100644 --- a/Makefile +++ b/Makefile @@ -7,25 +7,43 @@ PROGRAM_SOURCES = gsw_check_functions.c\ LIBRARY_SRCS = gsw_oceanographic_toolbox.c \ gsw_saar.c +# This includes TOOLS.gcc if make (unix) is used +# The #\ logic causes the include to be ignored by nmake (windows) +# nmake automatically includes TOOLS.ini # \ !ifdef 0 # include TOOLS.gcc # \ !endif # \ # +# --- NOTES ---: +# (1) The default CC variable is defined in the TOOLS.gcc file (make) / Tools.ini (nmake) file. +# use (n)make CC=compiler_command_of_choice to override the default compiler. +# (2) On windows the default CC variable is cl with the option /TP. This causes the project to be compiled +# as C++ code on windows. This is necessary because msvc does not support C99 style complex numbers. +# See also notes in TOOLS.ini. + + all: $(Program) $(Library) $(Program): - $(COMPILER) $(CRELEASE) $(PROGRAM_SOURCES) $(LIBS) $(OUT)$(Program)$(X) + $(CC) $(CRELEASE) $(PROGRAM_SOURCES) $(LIBS) $(OUT)$(Program)$(X) library: $(Library) $(Library): - $(COMPILER) $(LFLAGS) $(LIBRARY_SRCS) $(LIBS) $(OUT)$(Library)$(SHARED_POSTFIX) + $(CC) $(LFLAGS) $(LIBRARY_SRCS) $(LIBS) $(OUT)$(Library)$(SHARED_POSTFIX) clean: $(RM) *.o *.obj *.ilk *.pdb *.tmp *.i *~ $(RM) $(Library)$(SHARED_POSTFIX) $(RM) $(Program)$(X) +# Print the the CC variable (for debugging purpose). +# This is useful to check if the correct compiler is used with the specified commands. name_compiler: - @echo $(COMPILER_NAME) + @echo $(CC) + +# Print the the EXTRA_FLAGS variable (for debugging purpose). +# This is useful to check if the correct extra flags are used with the specified commands. +name_extra_flags: + @echo $(EXTRA_FLAGS) diff --git a/TOOLS.gcc b/TOOLS.gcc index 71a44e1..fa838b0 100644 --- a/TOOLS.gcc +++ b/TOOLS.gcc @@ -3,16 +3,13 @@ CDEBUG = -g LDEBUG = CRELEASE = -O3 LRELEASE = -CFLAGS = $(CRELEASE) -LFLAGS = -fPIC -shared $(LRELEASE) +EXTRA_FLAGS = +CFLAGS = $(CRELEASE) $(EXTRA_FLAGS) +LFLAGS = -fPIC -shared $(LRELEASE) $(EXTRA_FLAGS) OUT = -o RM = rm -f CP = cp CC = gcc -CPP = gcc X = O = .o SHARED_POSTFIX = .so - -# Optionally set the compiler using the environment variable COMPILER -COMPILER ?= $(CPP) diff --git a/TOOLS.ini b/TOOLS.ini index 8736bb7..6459dac 100644 --- a/TOOLS.ini +++ b/TOOLS.ini @@ -4,16 +4,25 @@ CDEBUG = /Zi LDEBUG = CRELEASE = /Ox LRELEASE = /Ox -CFLAGS = $(CRELEASE) -LFLAGS = /LD $(LRELEASE) +EXTRA_FLAGS = /TP +CFLAGS = $(CRELEASE) $(EXTRA_FLAGS) +LFLAGS = /LD $(LRELEASE) $(EXTRA_FLAGS) OUT = /Fe RM = del /F /Q CP = copy CC = cl -CPP = $(CC) /TP X = .exe O = .obj SHARED_POSTFIX = .dll -# set the compiler using the environment variable COMPILER -# to override with environment variable use nmake /E -COMPILER = $(CPP) + +# Notes: The EXTRA_FLAGS /TP causes the compiler to treat all .c files as C++ files +# This is necessary on windows because the msvc C compiler does not support +# standard c99 complex numbers. However, msvc does support the c++ standard +# version of complex numbers (). The project automatically detects +# if it is compiled in c or c++ mode and uses the appropriate header file. + +# If you want to use a different compiler (e.g clang-cl), you can set the CC variable +# using e.g. nmake CC=clang-cl. +# If this compiler is not compatible with the msvc compiler, you may need +# to remove the EXTRA_FLAGS /TP option by setting EXTRA_FLAGS to an empty string. +# e.g. nmake CC=gcc EXTRA_FLAGS=