Skip to content

Commit 41574fa

Browse files
committed
simplify what was ported from the makefiles
* remove some of the complicated logic. The additional warnings always make sense. CMake uses the state of `BUILD_SHARED_LIBS` to determine the library target type. Also remove the comment regarding building shared and static at the same time, as usually that's done as necessary by the user. * append user-defined {C,LD}FLAGS instead of preprending - we expect them to know what they do, so they can override our defaults * use target_{compile,link}_options() instead of setting variables Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 391e47d commit 41574fa

File tree

1 file changed

+36
-64
lines changed

1 file changed

+36
-64
lines changed

CMakeLists.txt

Lines changed: 36 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,38 @@ option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"
2020
# Compose CFLAGS
2121
#-----------------------------------------------------------------------------
2222

23-
# check if there was one already set.
24-
if(DEFINED ENV{LTM_CFLAGS})
25-
set(LTM_C_FLAGS $ENV{LTM_CFLAGS})
26-
endif()
27-
if(DEFINED ENV{LTM_LDFLAGS})
28-
set(LTM_LD_FLAGS $ENV{LTM_LDFLAGS})
29-
endif()
30-
3123
# Some information copied from makefile_include.mk
3224

3325
# Basic set
34-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wall -Wsign-compare -Wextra -Wshadow")
26+
set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow)
27+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align)
28+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith)
3529

3630
# Do we run the sanitizer?
3731
if(DEFINED ENV{SANITIZER})
38-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero")
39-
endif()
40-
41-
if(NOT DEFINED ENV{NO_ADDTL_WARNINGS})
42-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align")
43-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith")
32+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero)
4433
endif()
4534

4635
if(DEFINED ENV{CONV_WARNINGS})
47-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -std=c89 -Wconversion -Wsign-conversion")
36+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -std=c89 -Wconversion -Wsign-conversion)
4837
if($ENV{CONV_WARNINGS} EQUAL "strict")
49-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wc++-compat")
38+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wc++-compat)
5039
endif()
5140
else()
52-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wsystem-headers")
41+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wsystem-headers)
5342
endif()
5443

5544
if(DEFINED ENV{COMPILE_DEBUG})
56-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -g3")
45+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -g3)
5746
endif()
5847

5948
if(DEFINED ENV{COMPILE_SIZE})
60-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Os")
49+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Os)
6150
else()
6251
if(NOT DEFINED ENV{IGNORE_SPEED})
63-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -O3 -funroll-loops")
52+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -O3 -funroll-loops)
6453
#x86 optimizations [should be valid for any GCC install though]
65-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -fomit-frame-pointer")
54+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -fomit-frame-pointer)
6655
endif()
6756
# TODO:
6857
# if(DEFINED ENV{COMPILE_LTO})
@@ -72,70 +61,56 @@ else()
7261
# endif()
7362
endif()
7463

64+
# TODO
65+
# Are the coming three checks really the best way?
66+
7567
# What compiler do we have and what are their...uhm... peculiarities
7668
# TODO: is the check for a C++ compiler necessary?
7769
if( (CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang") OR (CMAKE_CXX_COMPILER_ID MATCHES "(C|c?)lang"))
78-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header")
70+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
7971
endif()
8072

8173
if( (CMAKE_C_COMPILER_ID MATCHES "mingw") OR (CMAKE_CXX_COMPILER_ID MATCHES "mingw"))
82-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-shadow")
74+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-shadow)
8375
endif()
8476

8577
if(DEFINED ENV{PLATFORM})
8678
if($ENV{PLATFORM} MATCHES "Darwin")
87-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-nullability-completeness")
79+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-nullability-completeness)
8880
endif()
8981
if($ENV{PLATFORM} MATCHES "CYGWIN")
90-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -no-undefined")
82+
set(LTM_C_FLAGS ${LTM_C_FLAGS} -no-undefined)
9183
endif()
9284
endif()
9385

9486
# TODO: coverage (lgcov)
9587

96-
# We have several private functions in the library and the "demo/test" programm
97-
# needs a littkle note to be able to switch them off. Please use the static build
98-
# to get a full test.
99-
if(BUILD_SHARED_LIBS)
100-
set(LTM_C_FLAGS "${LTM_C_FLAGS} -DLTM_TEST_DYNAMIC")
88+
# If the user set the environment variables at generate-time, append them
89+
# in order to allow overriding our defaults.
90+
if(DEFINED ENV{LTM_CFLAGS})
91+
set(LTM_C_FLAGS ${LTM_C_FLAGS} $ENV{LTM_CFLAGS})
92+
endif()
93+
if(DEFINED ENV{LTM_LDFLAGS})
94+
set(LTM_LD_FLAGS ${LTM_LD_FLAGS} $ENV{LTM_LDFLAGS})
10195
endif()
102-
103-
# Bring it home
104-
set(CMAKE_C_FLAGS "${LTM_C_FLAGS}")
105-
set(CMAKE_C_FLAGS_DEBUG "${LTM_C_FLAGS}")
106-
set(CMAKE_C_FLAGS_RELEASE "${LTM_C_FLAGS}")
10796

10897
#-----------------------------------------------------------------------------
10998
# library target
11099
#-----------------------------------------------------------------------------
111-
112-
# TODO: There may be a way but I found none to build both at once without complication.
113-
# It is possible with e.g. Linux where the static library is named libtommath.a
114-
# and the dynamic library libtommath.so*, two different names.
115-
# That is not the case with e.g. Windows where both types have the same name.
116-
# See also:
117-
# https://stackoverflow.com/questions/2152077/is-it-possible-to-get-cmake-to-build-both-a-static-and-shared-library-at-the-sam
118-
if(BUILD_SHARED_LIBS)
119-
add_library(${PROJECT_NAME} SHARED
120-
${SOURCES}
121-
)
122-
else()
123-
add_library(${PROJECT_NAME} STATIC
124-
${SOURCES}
125-
)
126-
endif()
127-
128-
# Clear cache manually
129-
unset(BUILD_SHARED_LIBS CACHE)
100+
add_library(${PROJECT_NAME}
101+
${SOURCES}
102+
)
130103

131104
target_include_directories(${PROJECT_NAME} PUBLIC
132105
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
133106
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
134107
)
135108

136-
target_include_directories(${PROJECT_NAME} PUBLIC
137-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
138-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
109+
target_compile_options(${PROJECT_NAME} BEFORE PRIVATE
110+
${LTM_C_FLAGS}
111+
)
112+
target_link_options(${PROJECT_NAME} BEFORE PRIVATE
113+
${LTM_LD_FLAGS}
139114
)
140115

141116
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
@@ -159,17 +134,13 @@ target_link_libraries(test-target PRIVATE
159134
${PROJECT_NAME}
160135
)
161136

162-
# for the SHARED_LIBRARY build we need some special flags enabled
163-
# We also allow our users to override our selection by defining their own
164-
# `CMAKE_C_FLAGS` on generation-phase. CMake itself doesn't allow a user
165-
# to override settings defined in the CMakeLists.txt so we append it manually
166-
# again even though CMake prepended it already.
167137
target_compile_options(test-target BEFORE PRIVATE
168138
$<$<STREQUAL:$<TARGET_PROPERTY:${PROJECT_NAME},TYPE>,SHARED_LIBRARY>:-O1 -DLTM_TEST_DYNAMIC>
169-
${CMAKE_C_FLAGS}
139+
${LTM_C_FLAGS}
170140
)
171141
target_link_options(test-target BEFORE PRIVATE
172142
$<$<STREQUAL:$<TARGET_PROPERTY:${PROJECT_NAME},TYPE>,SHARED_LIBRARY>:-O1>
143+
${LTM_LD_FLAGS}
173144
)
174145

175146
#-----------------------------------------------------------------------------
@@ -191,6 +162,7 @@ set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
191162

192163
install(TARGETS ${PROJECT_NAME}
193164
EXPORT ${TARGETS_EXPORT_NAME}
165+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
194166
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
195167
)
196168

0 commit comments

Comments
 (0)