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 6e333c6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 29 deletions.
43 changes: 27 additions & 16 deletions .github/workflows/test-alternative-compilers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,65 @@ 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_tool: "nmake /E" #/E causes environment variables to override makefile macro definitions.
- name: linux / g++ (c++)
os: ubuntu-latest
compiler: 'g++ -x c++'
compiler: 'g++'
extra_flags: '-x c++'
make_tool: "make"
- name: linux / clang (c)
os: ubuntu-latest
compiler: 'clang'
make_tool: "make"
- name: linux / clang (c++)
os: ubuntu-latest
compiler: 'clang -x c++'
compiler: 'clang'
extra_flags: '-x c++'
make_tool: "make"
- name: macos / g++ (c++)
os: macos-latest
compiler: 'g++ -x c++'
compiler: 'g++'
extra_flags: '-x c++'
make_tool: "make"
- name: macos / clang (c)
os: macos-latest
compiler: 'clang'
make_tool: "make"
- name: macos / clang (c++)
os: macos-latest
compiler: 'clang -x c++'
compiler: 'clang'
extra_flags: '-x c++'
make_tool: "make"
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_tool }} name_compiler)
extra_flags=$(${{ matrix.make_tool }} 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 }}

- name: Tests
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.
# On Nix (make) the default CC variable is automatically overwritten by the CC environment variable.
# On Windows (nmake) the default CC variable is only overwritten by the CC environment variable if nmake /E is called.
# (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 with the EXTRA_FLAGS used (for debugging purpose).
# This is useful to check if the correct compiler with the correct EXTRA_FLAGS is used.
# (e.g. the one from the environment or the one from the TOOLS.gcc/TOOLS.ini file)
name_compiler:
@echo $(COMPILER_NAME)
@echo $(CC)

name_extra_flags:
@echo $(EXTRA_FLAGS)
11 changes: 6 additions & 5 deletions TOOLS.gcc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ CDEBUG = -g
LDEBUG =
CRELEASE = -O3
LRELEASE =
CFLAGS = $(CRELEASE)
EXTRA_FLAGS ?=
CFLAGS = $(CRELEASE) $(EXTRA_FLAGS)
LFLAGS = -fPIC -shared $(LRELEASE)
OUT = -o
RM = rm -f
CP = cp
CC = gcc
CPP = gcc
CC ?= gcc
X =
O = .o
SHARED_POSTFIX = .so

# Optionally set the compiler using the environment variable COMPILER
COMPILER ?= $(CPP)
# Note:
# The default compiler CC and the EXTRA_FLAGS variables in this file are
# automatically overwritten by the CC and EXTRA_FLAGS environment variable
20 changes: 15 additions & 5 deletions TOOLS.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ CDEBUG = /Zi
LDEBUG =
CRELEASE = /Ox
LRELEASE = /Ox
CFLAGS = $(CRELEASE)
EXTRA_FLAGS = /TP
CFLAGS = $(CRELEASE) $(EXTRA_FLAGS)
LFLAGS = /LD $(LRELEASE)
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, you can set the CC environment
# variable and nmake /E. The /E option tells nmake to use the environment
# variables as the default values for the variables in the makefile.
# If this compiler is not compatible with the msvc compiler, you will need
# to also set the EXTRA_FLAGS variable to the appropriate flags for your
# compiler. (e.g. -x c++ for gcc)

0 comments on commit 6e333c6

Please sign in to comment.