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 <s@jaeckel.eu>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 972321c..3efd953 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -20,49 +20,38 @@ option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"
# Compose CFLAGS
#-----------------------------------------------------------------------------
-# check if there was one already set.
-if(DEFINED ENV{LTM_CFLAGS})
- set(LTM_C_FLAGS $ENV{LTM_CFLAGS})
-endif()
-if(DEFINED ENV{LTM_LDFLAGS})
- set(LTM_LD_FLAGS $ENV{LTM_LDFLAGS})
-endif()
-
# Some information copied from makefile_include.mk
# Basic set
-set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wall -Wsign-compare -Wextra -Wshadow")
+set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow)
+set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align)
+set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith)
# Do we run the sanitizer?
if(DEFINED ENV{SANITIZER})
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero")
-endif()
-
-if(NOT DEFINED ENV{NO_ADDTL_WARNINGS})
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align")
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -fsanitize=undefined -fno-sanitize-recover=all -fno-sanitize=float-divide-by-zero)
endif()
if(DEFINED ENV{CONV_WARNINGS})
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -std=c89 -Wconversion -Wsign-conversion")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -std=c89 -Wconversion -Wsign-conversion)
if($ENV{CONV_WARNINGS} EQUAL "strict")
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wc++-compat")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wc++-compat)
endif()
else()
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wsystem-headers")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wsystem-headers)
endif()
if(DEFINED ENV{COMPILE_DEBUG})
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -g3")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -g3)
endif()
if(DEFINED ENV{COMPILE_SIZE})
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Os")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -Os)
else()
if(NOT DEFINED ENV{IGNORE_SPEED})
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -O3 -funroll-loops")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -O3 -funroll-loops)
#x86 optimizations [should be valid for any GCC install though]
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -fomit-frame-pointer")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -fomit-frame-pointer)
endif()
# TODO:
# if(DEFINED ENV{COMPILE_LTO})
@@ -72,70 +61,56 @@ else()
# endif()
endif()
+# TODO
+# Are the coming three checks really the best way?
+
# What compiler do we have and what are their...uhm... peculiarities
# TODO: is the check for a C++ compiler necessary?
if( (CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang") OR (CMAKE_CXX_COMPILER_ID MATCHES "(C|c?)lang"))
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
endif()
if( (CMAKE_C_COMPILER_ID MATCHES "mingw") OR (CMAKE_CXX_COMPILER_ID MATCHES "mingw"))
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-shadow")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-shadow)
endif()
if(DEFINED ENV{PLATFORM})
if($ENV{PLATFORM} MATCHES "Darwin")
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -Wno-nullability-completeness")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-nullability-completeness)
endif()
if($ENV{PLATFORM} MATCHES "CYGWIN")
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -no-undefined")
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} -no-undefined)
endif()
endif()
# TODO: coverage (lgcov)
-# We have several private functions in the library and the "demo/test" programm
-# needs a littkle note to be able to switch them off. Please use the static build
-# to get a full test.
-if(BUILD_SHARED_LIBS)
- set(LTM_C_FLAGS "${LTM_C_FLAGS} -DLTM_TEST_DYNAMIC")
+# If the user set the environment variables at generate-time, append them
+# in order to allow overriding our defaults.
+if(DEFINED ENV{LTM_CFLAGS})
+ set(LTM_C_FLAGS ${LTM_C_FLAGS} $ENV{LTM_CFLAGS})
+endif()
+if(DEFINED ENV{LTM_LDFLAGS})
+ set(LTM_LD_FLAGS ${LTM_LD_FLAGS} $ENV{LTM_LDFLAGS})
endif()
-
-# Bring it home
-set(CMAKE_C_FLAGS "${LTM_C_FLAGS}")
-set(CMAKE_C_FLAGS_DEBUG "${LTM_C_FLAGS}")
-set(CMAKE_C_FLAGS_RELEASE "${LTM_C_FLAGS}")
#-----------------------------------------------------------------------------
# library target
#-----------------------------------------------------------------------------
-
-# TODO: There may be a way but I found none to build both at once without complication.
-# It is possible with e.g. Linux where the static library is named libtommath.a
-# and the dynamic library libtommath.so*, two different names.
-# That is not the case with e.g. Windows where both types have the same name.
-# See also:
-# https://stackoverflow.com/questions/2152077/is-it-possible-to-get-cmake-to-build-both-a-static-and-shared-library-at-the-sam
-if(BUILD_SHARED_LIBS)
- add_library(${PROJECT_NAME} SHARED
- ${SOURCES}
- )
-else()
- add_library(${PROJECT_NAME} STATIC
- ${SOURCES}
- )
-endif()
-
-# Clear cache manually
-unset(BUILD_SHARED_LIBS CACHE)
+add_library(${PROJECT_NAME}
+ ${SOURCES}
+)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
)
-target_include_directories(${PROJECT_NAME} PUBLIC
- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}>
+target_compile_options(${PROJECT_NAME} BEFORE PRIVATE
+ ${LTM_C_FLAGS}
+)
+target_link_options(${PROJECT_NAME} BEFORE PRIVATE
+ ${LTM_LD_FLAGS}
)
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
@@ -159,17 +134,13 @@ target_link_libraries(test-target PRIVATE
${PROJECT_NAME}
)
-# for the SHARED_LIBRARY build we need some special flags enabled
-# We also allow our users to override our selection by defining their own
-# `CMAKE_C_FLAGS` on generation-phase. CMake itself doesn't allow a user
-# to override settings defined in the CMakeLists.txt so we append it manually
-# again even though CMake prepended it already.
target_compile_options(test-target BEFORE PRIVATE
$<$<STREQUAL:$<TARGET_PROPERTY:${PROJECT_NAME},TYPE>,SHARED_LIBRARY>:-O1 -DLTM_TEST_DYNAMIC>
- ${CMAKE_C_FLAGS}
+ ${LTM_C_FLAGS}
)
target_link_options(test-target BEFORE PRIVATE
$<$<STREQUAL:$<TARGET_PROPERTY:${PROJECT_NAME},TYPE>,SHARED_LIBRARY>:-O1>
+ ${LTM_LD_FLAGS}
)
#-----------------------------------------------------------------------------
@@ -191,6 +162,7 @@ set(TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
install(TARGETS ${PROJECT_NAME}
EXPORT ${TARGETS_EXPORT_NAME}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)