-
Notifications
You must be signed in to change notification settings - Fork 207
Add cmake support #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add cmake support #528
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f993147
git: ignore cmake build directories
mrpelotazo 44b6517
git: ignore kdevelop generated files
mrpelotazo 019c3dc
cmake: add cmake lists file
mrpelotazo 8fd791d
cmake: have the sources/headers variables defined in a separated file
mrpelotazo 6da9d1a
cmake: rename test target
mrpelotazo 3c94017
cmake: add cpack for generating packages
mrpelotazo 7f9fbb6
helper.pl: add support to update sources.cmake
sjaeckel 97ba3e1
cmake: update cmake sources file
mrpelotazo cc8614a
cmake: define BUILD_SHARED_LIBS option...
mrpelotazo 0a37084
cmake: add missing LIBRARY DESTINATION in install targets
mrpelotazo 9412d1a
update README
sjaeckel e1788a8
allow unit tests in shared library builds
sjaeckel 7cacded
add cmake tests to CI
sjaeckel 391e47d
Made "test" work with shared lib, started gathering environment varia…
czurnieden 41574fa
simplify what was ported from the makefiles
sjaeckel cb8f212
add SPDX identifiers
sjaeckel ba74457
Build and install libtommath.pc
czurnieden 22dfda7
further simplifications/improvements
sjaeckel ed6ad7d
extend CI matrix for different cmake options
sjaeckel ab9ba06
fix `pkgconfig` creation in `makefile.shared`
sjaeckel a107eaf
include all headers in sources.cmake
sjaeckel 0b98bc7
split up into two CMakeLists.txt
sjaeckel 0f80b46
adjust CI builds
sjaeckel d31801f
take review comments into account
sjaeckel 72ce1e5
rename cmake project to `libtommath`
sjaeckel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,3 +104,10 @@ logs/*.png | |
logs/*-*.dem | ||
|
||
callgraph.txt | ||
|
||
# cmake build directories | ||
build*/ | ||
|
||
# kdevelop section | ||
.kdev4/ | ||
*.kdev4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,271 @@ | ||
# SPDX-License-Identifier: Unlicense | ||
# | ||
# LibTomMath, a free open source portable number theoretic multiple-precision | ||
# integer (MPI) library written entirely in C. | ||
# | ||
|
||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(libtommath | ||
VERSION 1.2.0 | ||
DESCRIPTION "A free open source portable number theoretic multiple-precision integer (MPI) library written entirely in C." | ||
HOMEPAGE_URL "https://www.libtom.net/LibTomMath" | ||
LANGUAGES C) | ||
|
||
# package release version | ||
# bump if re-releasing the same VERSION + patches | ||
# set to 1 if releasing a new VERSION | ||
set(PACKAGE_RELEASE_VERSION 1) | ||
|
||
#----------------------------------------------------------------------------- | ||
# Include cmake modules | ||
#----------------------------------------------------------------------------- | ||
include(GNUInstallDirs) | ||
include(CheckIPOSupported) | ||
include(CMakePackageConfigHelpers) | ||
# default is "No tests" | ||
option(BUILD_TESTING "" OFF) | ||
include(CTest) | ||
include(sources.cmake) | ||
|
||
# The only direct cmake argument for now | ||
option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"ON\", default is static" OFF) | ||
|
||
#----------------------------------------------------------------------------- | ||
# Compose CFLAGS | ||
#----------------------------------------------------------------------------- | ||
|
||
# Some information ported from makefile_include.mk | ||
|
||
|
||
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) | ||
message(STATUS "Setting build type to 'Release' as none was specified.") | ||
set(CMAKE_BUILD_TYPE "Release") | ||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
endif() | ||
|
||
# We only differentiate between MSVC and GCC-compatible compilers | ||
if(MSVC) | ||
set(LTM_C_FLAGS -W3) | ||
else() | ||
set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow | ||
-Wdeclaration-after-statement -Wbad-function-cast -Wcast-align | ||
-Wstrict-prototypes -Wpointer-arith -Wsystem-headers) | ||
set(CMAKE_C_FLAGS_DEBUG "-g3") | ||
set(CMAKE_C_FLAGS_RELEASE "-O3 -funroll-loops -fomit-frame-pointer") | ||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g3 -O2") | ||
set(CMAKE_C_FLAGS_MINSIZEREL "-Os") | ||
endif() | ||
|
||
# What compiler do we have and what are their...uhm... peculiarities | ||
if(CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang") | ||
list(APPEND LTM_C_FLAGS -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header) | ||
# Clang requires at least '-O1' for dead code eliminiation | ||
set(CMAKE_C_FLAGS_DEBUG "-O1 ${CMAKE_C_FLAGS_DEBUG}") | ||
endif() | ||
if(CMAKE_C_COMPILER MATCHES "mingw") | ||
list(APPEND LTM_C_FLAGS -Wno-shadow -Wno-expansion-to-defined -Wno-declaration-after-statement -Wno-bad-function-cast) | ||
endif() | ||
if(CMAKE_SYSTEM_NAME MATCHES "Darwin") | ||
list(APPEND LTM_C_FLAGS -Wno-nullability-completeness) | ||
endif() | ||
if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN") | ||
list(APPEND LTM_C_FLAGS -no-undefined) | ||
endif() | ||
|
||
# TODO: coverage (lgcov) | ||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# If the user set the environment variables at generate-time, append them | ||
# in order to allow overriding our defaults. | ||
# ${LTM_CFLAGS} means the user passed it via sth like: | ||
# $ cmake -DLTM_CFLAGS="foo" | ||
list(APPEND LTM_C_FLAGS ${LTM_CFLAGS}) | ||
list(APPEND LTM_LD_FLAGS ${LTM_LDFLAGS}) | ||
|
||
#----------------------------------------------------------------------------- | ||
# library target | ||
#----------------------------------------------------------------------------- | ||
add_library(${PROJECT_NAME} | ||
${SOURCES} | ||
${HEADERS} | ||
) | ||
|
||
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 | ||
OUTPUT_NAME tommath | ||
VERSION ${PROJECT_VERSION} | ||
SOVERSION ${PROJECT_VERSION_MAJOR} | ||
PUBLIC_HEADER tommath.h | ||
) | ||
|
||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
option(COMPILE_LTO "Build with LTO enabled") | ||
if(COMPILE_LTO) | ||
check_ipo_supported(RESULT COMPILER_SUPPORTS_LTO) | ||
if(COMPILER_SUPPORTS_LTO) | ||
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) | ||
else() | ||
message(SEND_ERROR "This compiler does not support LTO. Reconfigure ${PROJECT_NAME} with -DCOMPILE_LTO=OFF.") | ||
endif() | ||
endif() | ||
|
||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#----------------------------------------------------------------------------- | ||
# demo target | ||
#----------------------------------------------------------------------------- | ||
|
||
if(BUILD_TESTING) | ||
enable_testing() | ||
add_subdirectory(demo) | ||
endif() | ||
|
||
#----------------------------------------------------------------------------- | ||
# Install/export targets and files | ||
#----------------------------------------------------------------------------- | ||
set(CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") | ||
set(PROJECT_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake") | ||
set(PROJECT_CONFIG_FILE "${PROJECT_NAME}-config.cmake") | ||
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} | ||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME} | ||
) | ||
|
||
# Install libtommath.pc for pkg-config if we build a shared library | ||
if(BUILD_SHARED_LIBS) | ||
# Let the user override the default directory of the pkg-config file (usually this shouldn't be required to be changed) | ||
set(CMAKE_INSTALL_PKGCONFIGDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Folder where to install .pc files") | ||
|
||
configure_file( | ||
${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pc.in | ||
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc | ||
@ONLY | ||
) | ||
|
||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc | ||
DESTINATION ${CMAKE_INSTALL_PKGCONFIGDIR} | ||
) | ||
endif() | ||
|
||
# generate package version file | ||
write_basic_package_version_file( | ||
${PROJECT_VERSION_FILE} | ||
VERSION ${PROJECT_VERSION} | ||
COMPATIBILITY SameMajorVersion | ||
) | ||
|
||
# install version file | ||
install(FILES ${PROJECT_VERSION_FILE} | ||
DESTINATION ${CONFIG_INSTALL_DIR} | ||
) | ||
|
||
# build directory package config | ||
export(EXPORT ${TARGETS_EXPORT_NAME} | ||
FILE ${PROJECT_CONFIG_FILE} | ||
) | ||
|
||
# installed package config | ||
install(EXPORT ${TARGETS_EXPORT_NAME} | ||
DESTINATION ${CONFIG_INSTALL_DIR} | ||
FILE ${PROJECT_CONFIG_FILE} | ||
) | ||
|
||
# add to CMake registry | ||
export(PACKAGE ${PROJECT_NAME}) | ||
|
||
#--------------------------------------------------------------------------------------- | ||
# Create release packages | ||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#--------------------------------------------------------------------------------------- | ||
|
||
# determine distribution and architecture | ||
find_program(LSB_RELEASE lsb_release) | ||
find_program(SYSCTL sysctl) | ||
find_program(UNAME uname) | ||
|
||
if(UNAME) | ||
execute_process(COMMAND uname -m OUTPUT_VARIABLE MACHINE_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
elseif(SYSCTL) | ||
execute_process(COMMAND sysctl -b hw.machine_arch OUTPUT_VARIABLE MACHINE_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
else() | ||
string(TOLOWER ${CMAKE_SYSTEM_NAME} MACHINE_ARCH) | ||
endif() | ||
|
||
if(LSB_RELEASE) | ||
execute_process(COMMAND lsb_release -si OUTPUT_VARIABLE LINUX_DISTRO OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
execute_process(COMMAND lsb_release -sc OUTPUT_VARIABLE LINUX_DISTRO_CODENAME OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
execute_process(COMMAND lsb_release -sr OUTPUT_VARIABLE LINUX_DISTRO_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
|
||
string(TOLOWER ${LINUX_DISTRO} LINUX_DISTRO) | ||
if(LINUX_DISTRO_CODENAME STREQUAL "n/a") | ||
set(DISTRO_PACK_PATH ${LINUX_DISTRO}/${LINUX_DISTRO_VERSION}/) | ||
else() | ||
set(DISTRO_PACK_PATH ${LINUX_DISTRO}/${LINUX_DISTRO_CODENAME}/) | ||
endif() | ||
else() | ||
set(DISTRO_PACK_PATH ${CMAKE_SYSTEM_NAME}/) | ||
endif() | ||
|
||
# default CPack generators | ||
set(CPACK_GENERATOR TGZ STGZ) | ||
|
||
# extra CPack generators | ||
if(LINUX_DISTRO STREQUAL "debian" OR LINUX_DISTRO STREQUAL "ubuntu" OR LINUX_DISTRO STREQUAL "linuxmint") | ||
list(APPEND CPACK_GENERATOR DEB) | ||
elseif(LINUX_DISTRO STREQUAL "fedora" OR LINUX_DISTRO STREQUAL "opensuse" OR LINUX_DISTRO STREQUAL "centos") | ||
list(APPEND CPACK_GENERATOR RPM) | ||
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") | ||
list(APPEND CPACK_GENERATOR FREEBSD) | ||
endif() | ||
|
||
# general CPack config | ||
set(CPACK_PACKAGE_DIRECTORY ${CMAKE_BINARY_DIR}/packages/${DISTRO_PACK_PATH}) | ||
message(STATUS "CPack: packages will be generated under ${CPACK_PACKAGE_DIRECTORY}") | ||
if(BUILD_SHARED_LIBS) | ||
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}${PROJECT_VERSION_MAJOR}") | ||
else() | ||
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}-devel") | ||
endif() | ||
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) | ||
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LibTomMath") | ||
set(CPACK_PACKAGE_VENDOR "libtom projects") | ||
set(CPACK_PACKAGE_CONTACT "[email protected]") | ||
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE") | ||
set(PACKAGE_NAME_TRAILER ${CPACK_PACKAGE_VERSION}-${PACKAGE_RELEASE_VERSION}_${MACHINE_ARCH}) | ||
set(CPACK_PACKAGE_FILE_NAME ${CPACK_PACKAGE_NAME}-${PACKAGE_NAME_TRAILER}) | ||
set(CPACK_STRIP_FILES ON) | ||
|
||
# deb specific CPack config | ||
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) | ||
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) | ||
set(CPACK_DEBIAN_PACKAGE_RELEASE ${PACKAGE_RELEASE_VERSION}) | ||
if(BUILD_SHARED_LIBS) | ||
set(CPACK_DEBIAN_PACKAGE_SECTION "libs") | ||
else() | ||
set(CPACK_DEBIAN_PACKAGE_NAME "${PROJECT_NAME}-dev") | ||
set(CPACK_DEBIAN_PACKAGE_SECTION "devel") | ||
endif() | ||
|
||
# rpm specific CPack config | ||
set(CPACK_RPM_PACKAGE_RELEASE ${PACKAGE_RELEASE_VERSION}) | ||
set(CPACK_RPM_PACKAGE_ARCHITECTURE ${MACHINE_ARCH}) | ||
set(CPACK_RPM_PACKAGE_NAME "${CPACK_PACKAGE_NAME}-${PROJECT_VERSION}") | ||
set(CPACK_RPM_PACKAGE_LICENSE "The Unlicense") | ||
|
||
# FreeBSD specific CPack config | ||
set(CPACK_FREEBSD_PACKAGE_MAINTAINER "[email protected]") | ||
set(CPACK_FREEBSD_PACKAGE_ORIGIN "math/libtommath") | ||
set(CPACK_FREEBSD_PACKAGE_CATEGORIES "math") | ||
|
||
include(CPack) | ||
sjaeckel marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.