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 032c2b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
13 changes: 6 additions & 7 deletions .github/workflows/test-alternative-compilers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
include:
- name: windows / clang-cl (c++)
os: windows-latest
compiler: 'clang-cl /TP'
make_tool: "nmake"
compiler: 'clang-cl /TP' # /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++'
Expand All @@ -42,26 +42,25 @@ jobs:
make_tool: "make"
fail-fast: false

env:
COMPILER: ${{ matrix.compiler }}

steps:
- uses: actions/checkout@v4
- uses: ilammy/msvc-dev-cmd@v1

- name: Check COMPILER 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
exit 1
fi
- name: Compile
shell: bash
env:
COMPILER: ${{ matrix.compiler }}
run: ${{ matrix.make_tool }}

- name: Tests
Expand Down
23 changes: 19 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,40 @@ 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)

name_compiler:
@echo $(COMPILER_NAME)
# Print the the CC variable used (for debugging purpose).
# This is useful to check if the correct compiler is used. (e.g. the one from the
# environment or the one from the TOOLS.gcc/TOOLS.ini file)
name_CC:
@echo $(CC)
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 032c2b4

Please sign in to comment.