Skip to content

Commit 05679a6

Browse files
ximinezseelabs
authored andcommitted
Fix CMake ordering to find correct compiler:
* `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER` must be defined before `project`. However, it will clear `CMAKE_BUILD_TYPE`. Use `CACHE` variables and reorder some code to work around these constraints. * Also correct a couple of copy paste errors.
1 parent 44a9d42 commit 05679a6

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

Builds/CMake/CMakeFuncs.cmake

+35-8
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ macro(parse_target)
3333
if (${cur_component} STREQUAL gcc)
3434
if (DEFINED ENV{GNU_CC})
3535
set(CMAKE_C_COMPILER $ENV{GNU_CC})
36-
elseif ($ENV{CXX} MATCHES .*gcc.*)
37-
set(CMAKE_CXX_COMPILER $ENV{CC})
36+
elseif ($ENV{CC} MATCHES .*gcc.*)
37+
set(CMAKE_C_COMPILER $ENV{CC})
3838
else()
3939
find_program(CMAKE_C_COMPILER gcc)
4040
endif()
4141

4242
if (DEFINED ENV{GNU_CXX})
43-
set(CMAKE_C_COMPILER $ENV{GNU_CXX})
43+
set(CMAKE_CXX_COMPILER $ENV{GNU_CXX})
4444
elseif ($ENV{CXX} MATCHES .*g\\+\\+.*)
45-
set(CMAKE_C_COMPILER $ENV{CC})
45+
set(CMAKE_CXX_COMPILER $ENV{CXX})
4646
else()
4747
find_program(CMAKE_CXX_COMPILER g++)
4848
endif()
@@ -51,16 +51,16 @@ macro(parse_target)
5151
if (${cur_component} STREQUAL clang)
5252
if (DEFINED ENV{CLANG_CC})
5353
set(CMAKE_C_COMPILER $ENV{CLANG_CC})
54-
elseif ($ENV{CXX} MATCHES .*clang.*)
55-
set(CMAKE_CXX_COMPILER $ENV{CC})
54+
elseif ($ENV{CC} MATCHES .*clang.*)
55+
set(CMAKE_C_COMPILER $ENV{CC})
5656
else()
5757
find_program(CMAKE_C_COMPILER clang)
5858
endif()
5959

6060
if (DEFINED ENV{CLANG_CXX})
61-
set(CMAKE_C_COMPILER $ENV{CLANG_CXX})
61+
set(CMAKE_CXX_COMPILER $ENV{CLANG_CXX})
6262
elseif ($ENV{CXX} MATCHES .*clang.*)
63-
set(CMAKE_C_COMPILER $ENV{CC})
63+
set(CMAKE_CXX_COMPILER $ENV{CXX})
6464
else()
6565
find_program(CMAKE_CXX_COMPILER clang++)
6666
endif()
@@ -105,16 +105,40 @@ macro(parse_target)
105105
endif()
106106

107107
endwhile()
108+
# Promote these values to the CACHE, then unset the locals
109+
# to prevent shadowing.
110+
set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER} CACHE FILEPATH
111+
"Path to a program" FORCE)
112+
unset(CMAKE_C_COMPILER)
113+
set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE FILEPATH
114+
"Path to a program" FORCE)
115+
unset(CMAKE_CXX_COMPILER)
108116

109117
if (release)
110118
set(CMAKE_BUILD_TYPE Release)
111119
else()
112120
set(CMAKE_BUILD_TYPE Debug)
113121
endif()
114122

123+
# ensure that the unity flags are set and exclusive
124+
if (NOT DEFINED unity OR unity)
125+
# Default to unity builds
126+
set(unity true)
127+
set(nonunity false)
128+
else()
129+
set(unity false)
130+
set(nonunity true)
131+
endif()
132+
115133
if (NOT unity)
116134
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}Classic)
117135
endif()
136+
# Promote this value to the CACHE, then unset the local
137+
# to prevent shadowing.
138+
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE INTERNAL
139+
"Choose the type of build, options are in CMAKE_CONFIGURATION_TYPES"
140+
FORCE)
141+
unset(CMAKE_BUILD_TYPE)
118142
endif()
119143

120144
endmacro()
@@ -149,8 +173,11 @@ macro(setup_build_cache)
149173
ReleaseClassic)
150174
endif()
151175

176+
# Promote this value to the CACHE, then unset the local
177+
# to prevent shadowing.
152178
set(CMAKE_CONFIGURATION_TYPES
153179
${CMAKE_CONFIGURATION_TYPES} CACHE STRING "" FORCE)
180+
unset(CMAKE_CONFIGURATION_TYPES)
154181
endmacro()
155182

156183
############################################################

CMakeLists.txt

+14-15
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,22 @@
5252

5353
############################################################
5454

55+
#########################################################
56+
# CMAKE_C_COMPILER and CMAKE_CXX_COMPILER must be defined
57+
# before the project statement; However, the project
58+
# statement will clear CMAKE_BUILD_TYPE. CACHE variables,
59+
# along with the order of this code, are used to work
60+
# around these constraints.
61+
#
62+
# Don't put any code above or in this block, unless it
63+
# has similar constraints.
5564
cmake_minimum_required(VERSION 3.1.0)
56-
57-
# The project command can override some computed values.
58-
# Don't put any code above this line.
65+
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Builds/CMake")
66+
include(CMakeFuncs)
67+
set(openssl_min 1.0.2)
68+
parse_target()
5969
project(rippled)
70+
#########################################################
6071

6172
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_BINARY_DIR}")
6273
set(dir "build")
@@ -86,18 +97,6 @@ if("${CMAKE_GENERATOR}" MATCHES "Visual Studio" AND
8697
-G\"${CMAKE_GENERATOR} Win64\"")
8798
endif()
8899

89-
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Builds/CMake")
90-
include(CMakeFuncs)
91-
92-
set(openssl_min 1.0.2)
93-
94-
parse_target()
95-
96-
if (NOT DEFINED unity)
97-
set(unity true)
98-
set(nonunity false)
99-
endif()
100-
101100
setup_build_cache()
102101

103102
if(nonunity)

0 commit comments

Comments
 (0)