Skip to content

Commit 1fb5c8f

Browse files
committed
take review comments into account
* protect GCC-specific stuff * use `list(APPEND...)` * use CMake-style way to choose whether LTO should/can be done or not * only install public header, not all * add correct `install` option for DLL's on Windows * use correct folder for .pc files * check if `uname` exists Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 0f80b46 commit 1fb5c8f

File tree

1 file changed

+51
-27
lines changed

1 file changed

+51
-27
lines changed

CMakeLists.txt

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ set(PACKAGE_RELEASE_VERSION 1)
2121
# Include cmake modules
2222
#-----------------------------------------------------------------------------
2323
include(GNUInstallDirs)
24+
include(CheckIPOSupported)
2425
include(CMakePackageConfigHelpers)
26+
# default is "No tests"
27+
option(BUILD_TESTING "" OFF)
28+
include(CTest)
2529
include(sources.cmake)
2630

2731
# The only direct cmake argument for now
@@ -31,42 +35,41 @@ option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"
3135
# Compose CFLAGS
3236
#-----------------------------------------------------------------------------
3337

34-
# Some information copied from makefile_include.mk
38+
# Some information ported from makefile_include.mk
3539

36-
# Basic set
37-
set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow)
38-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align)
39-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith -Wsystem-headers)
4040

4141
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
4242
message(STATUS "Setting build type to 'Release' as none was specified.")
4343
set(CMAKE_BUILD_TYPE "Release")
4444
endif()
4545

46-
set(CMAKE_C_FLAGS_DEBUG "-g3")
47-
set(CMAKE_C_FLAGS_RELEASE "-O3 -funroll-loops -fomit-frame-pointer")
48-
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g3 -O2")
49-
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
50-
51-
if(COMPILE_LTO)
52-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -flto)
53-
set(LTM_LD_FLAGS ${LTM_LD_FLAGS} -flto)
46+
# We only differentiate between MSVC and GCC-compatible compilers
47+
if(MSVC)
48+
set(LTM_C_FLAGS -W3)
49+
else()
50+
set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow
51+
-Wdeclaration-after-statement -Wbad-function-cast -Wcast-align
52+
-Wstrict-prototypes -Wpointer-arith -Wsystem-headers)
53+
set(CMAKE_C_FLAGS_DEBUG "-g3")
54+
set(CMAKE_C_FLAGS_RELEASE "-O3 -funroll-loops -fomit-frame-pointer")
55+
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g3 -O2")
56+
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
5457
endif()
5558

5659
# What compiler do we have and what are their...uhm... peculiarities
5760
if(CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang")
58-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
61+
list(APPEND LTM_C_FLAGS -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
5962
# Clang requires at least '-O1' for dead code eliminiation
60-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O1")
63+
set(CMAKE_C_FLAGS_DEBUG "-O1 ${CMAKE_C_FLAGS_DEBUG}")
6164
endif()
6265
if(CMAKE_C_COMPILER MATCHES "mingw")
63-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-shadow)
66+
list(APPEND LTM_C_FLAGS -Wno-shadow -Wno-expansion-to-defined -Wno-declaration-after-statement -Wno-bad-function-cast)
6467
endif()
6568
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
66-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-nullability-completeness)
69+
list(APPEND LTM_C_FLAGS -Wno-nullability-completeness)
6770
endif()
6871
if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN")
69-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -no-undefined)
72+
list(APPEND LTM_C_FLAGS -no-undefined)
7073
endif()
7174

7275
# TODO: coverage (lgcov)
@@ -75,14 +78,15 @@ endif()
7578
# in order to allow overriding our defaults.
7679
# ${LTM_CFLAGS} means the user passed it via sth like:
7780
# $ cmake -DLTM_CFLAGS="foo"
78-
set(LTM_C_FLAGS ${LTM_C_FLAGS} ${LTM_CFLAGS})
79-
set(LTM_LD_FLAGS ${LTM_LD_FLAGS} ${LTM_LDFLAGS})
81+
list(APPEND LTM_C_FLAGS ${LTM_CFLAGS})
82+
list(APPEND LTM_LD_FLAGS ${LTM_LDFLAGS})
8083

8184
#-----------------------------------------------------------------------------
8285
# library target
8386
#-----------------------------------------------------------------------------
8487
add_library(${PROJECT_NAME}
8588
${SOURCES}
89+
${HEADERS}
8690
)
8791

8892
target_include_directories(${PROJECT_NAME} PUBLIC
@@ -100,13 +104,27 @@ target_link_options(${PROJECT_NAME} BEFORE PRIVATE
100104
set_target_properties(${PROJECT_NAME} PROPERTIES
101105
VERSION ${PROJECT_VERSION}
102106
SOVERSION ${PROJECT_VERSION_MAJOR}
107+
PUBLIC_HEADER tommath.h
103108
)
104109

110+
option(COMPILE_LTO "Build with LTO enabled")
111+
if(COMPILE_LTO)
112+
check_ipo_supported(RESULT COMPILER_SUPPORTS_LTO)
113+
if(COMPILER_SUPPORTS_LTO)
114+
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
115+
else()
116+
message(SEND_ERROR "This compiler does not support LTO. Reconfigure ${PROJECT_NAME} with -DCOMPILE_LTO=OFF.")
117+
endif()
118+
endif()
105119

106120
#-----------------------------------------------------------------------------
107121
# demo target
108122
#-----------------------------------------------------------------------------
109-
add_subdirectory(demo)
123+
124+
if(BUILD_TESTING)
125+
enable_testing()
126+
add_subdirectory(demo)
127+
endif()
110128

111129
#-----------------------------------------------------------------------------
112130
# Install/export targets and files
@@ -120,22 +138,23 @@ install(TARGETS ${PROJECT_NAME}
120138
EXPORT ${TARGETS_EXPORT_NAME}
121139
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122140
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
123-
)
124-
125-
install(FILES ${HEADERS}
126-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
141+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
142+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
127143
)
128144

129145
# Install libtommath.pc for pkg-config if we build a shared library
130146
if(BUILD_SHARED_LIBS)
147+
# Let the user override the default directory of the pkg-config file (usually this shouldn't be required to be changed)
148+
set(CMAKE_INSTALL_PKGCONFIGDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Folder where to install .pc files")
149+
131150
configure_file(
132151
${CMAKE_CURRENT_SOURCE_DIR}/lib${PROJECT_NAME}.pc.in
133152
${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.pc
134153
@ONLY
135154
)
136155

137156
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.pc
138-
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
157+
DESTINATION ${CMAKE_INSTALL_PKGCONFIGDIR}
139158
)
140159
endif()
141160

@@ -170,9 +189,14 @@ export(PACKAGE ${PROJECT_NAME})
170189
#---------------------------------------------------------------------------------------
171190

172191
# determine distribution and architecture
192+
find_program(UNAME uname)
173193
find_program(LSB_RELEASE lsb_release)
174194

175-
execute_process(COMMAND uname -m OUTPUT_VARIABLE MACHINE_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
195+
if(UNAME)
196+
execute_process(COMMAND uname -m OUTPUT_VARIABLE MACHINE_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
197+
else()
198+
string(TOLOWER ${CMAKE_SYSTEM_NAME} MACHINE_ARCH)
199+
endif()
176200

177201
if(LSB_RELEASE)
178202
execute_process(COMMAND lsb_release -si OUTPUT_VARIABLE LINUX_DISTRO OUTPUT_STRIP_TRAILING_WHITESPACE)

0 commit comments

Comments
 (0)