Skip to content

Commit bca60c7

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 <s@jaeckel.eu>
1 parent c924d4d commit bca60c7

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

CMakeLists.txt

Lines changed: 50 additions & 28 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.")
43-
set(CMAKE_BUILD_TYPE "Release")
43+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type")
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,25 @@ 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+
check_ipo_supported(RESULT result)
112+
if(result AND COMPILE_LTO)
113+
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
114+
elseif(COMPILE_LTO)
115+
message(WARNING "LTO is not supported for this compiler.")
116+
endif()
105117

106118
#-----------------------------------------------------------------------------
107119
# demo target
108120
#-----------------------------------------------------------------------------
109-
add_subdirectory(demo)
121+
122+
if(BUILD_TESTING)
123+
enable_testing()
124+
add_subdirectory(demo)
125+
endif()
110126

111127
#-----------------------------------------------------------------------------
112128
# Install/export targets and files
@@ -120,22 +136,23 @@ install(TARGETS ${PROJECT_NAME}
120136
EXPORT ${TARGETS_EXPORT_NAME}
121137
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122138
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
123-
)
124-
125-
install(FILES ${HEADERS}
126-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
139+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
140+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
127141
)
128142

129143
# Install libtommath.pc for pkg-config if we build a shared library
130144
if(BUILD_SHARED_LIBS)
145+
# Let the user override the default directory of the pkg-config file (usually this shouldn't be required to be changed)
146+
set(CMAKE_INSTALL_PKGCONFIGDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Folder where to install .pc files")
147+
131148
configure_file(
132149
${CMAKE_CURRENT_SOURCE_DIR}/lib${PROJECT_NAME}.pc.in
133150
${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.pc
134151
@ONLY
135152
)
136153

137154
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/lib${PROJECT_NAME}.pc
138-
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig
155+
DESTINATION ${CMAKE_INSTALL_PKGCONFIGDIR}
139156
)
140157
endif()
141158

@@ -170,9 +187,14 @@ export(PACKAGE ${PROJECT_NAME})
170187
#---------------------------------------------------------------------------------------
171188

172189
# determine distribution and architecture
190+
find_program(UNAME uname)
173191
find_program(LSB_RELEASE lsb_release)
174192

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

177199
if(LSB_RELEASE)
178200
execute_process(COMMAND lsb_release -si OUTPUT_VARIABLE LINUX_DISTRO OUTPUT_STRIP_TRAILING_WHITESPACE)

0 commit comments

Comments
 (0)