Skip to content

Commit

Permalink
Use CC variable in the Makefile directly
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-urban committed Sep 17, 2024
1 parent b3ad728 commit 8af633b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 38 deletions.
57 changes: 34 additions & 23 deletions .github/workflows/test-alternative-compilers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 3 additions & 6 deletions TOOLS.gcc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
21 changes: 15 additions & 6 deletions TOOLS.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<complex>). 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=

0 comments on commit 8af633b

Please sign in to comment.