-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
148 lines (124 loc) · 5.75 KB
/
CMakeLists.txt
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
CMAKE_MINIMUM_REQUIRED(VERSION 3.10.2)
PROJECT(lib-snowflakeid C)
# Settings
SET(CMAKE_C_STANDARD 99)
IF (MSVC)
SET(CMAKE_C_FLAGS "-Wall")
ELSE (MSVC)
SET(CMAKE_C_FLAGS "-Wall -Werror -pedantic -fPIC")
ENDIF (MSVC)
SET(CMAKE_CXX_FLAGS_DEBUG "-g")
SET(CMAKE_CXX_FLAGS_RELEASE "-O2")
# "LEADING_ZERO" function
SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules)
INCLUDE(LeadingZero)
# Configure version.h file
SET(LIBSNOWFLAKEID_VERSION_MAJOR "1")
SET(LIBSNOWFLAKEID_VERSION_MINOR "3")
SET(LIBSNOWFLAKEID_VERSION_PATCH "0")
LEADING_ZERO(PADDED_VERSION_MINOR 3 ${LIBSNOWFLAKEID_VERSION_MINOR})
LEADING_ZERO(PADDED_VERSION_PATCH 3 ${LIBSNOWFLAKEID_VERSION_PATCH})
SET(LIBSNOWFLAKEID_VERSION_STRING
"${LIBSNOWFLAKEID_VERSION_MAJOR}.${LIBSNOWFLAKEID_VERSION_MINOR}.${LIBSNOWFLAKEID_VERSION_PATCH}")
SET(LIBSNOWFLAKEID_VERSION_NUMBER "${LIBSNOWFLAKEID_VERSION_MAJOR}${PADDED_VERSION_MINOR}${PADDED_VERSION_PATCH}")
CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/res/version.h.template ${CMAKE_SOURCE_DIR}/inc/version.h)
# Display build type
IF (NOT CMAKE_BUILD_TYPE)
MESSAGE(STATUS "Build type: -")
ELSE (NOT CMAKE_BUILD_TYPE)
MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
ENDIF (NOT CMAKE_BUILD_TYPE)
# Detect "mutex" to use (WINDOWS or PTHREAD)
FIND_PACKAGE(Threads REQUIRED)
IF (CMAKE_USE_PTHREADS_INIT)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUSE_PTHREAD_MUTEX=1")
ELSEIF (CMAKE_USE_WIN32_THREADS_INIT)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DUSE_WINDOWS_MUTEX=1")
ENDIF (CMAKE_USE_PTHREADS_INIT)
# Header directories
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/inc")
# Source Files
SET(SOURCE_FILES ${SOURCE_FILES}
src/snowflakeid_destroy.c
src/snowflakeid_get_version_as_int.c
src/snowflakeid_get_version_as_str.c
src/snowflakeid_initialize.c
src/snowflakeid_next_value.c)
# Test Files
SET(TEST_FILES ${TEST_FILES}
test/test_main.c)
# Library
ADD_LIBRARY(lib-snowflakeid-obj OBJECT ${SOURCE_FILES})
ADD_LIBRARY(lib-snowflakeid-static STATIC $<TARGET_OBJECTS:lib-snowflakeid-obj>)
ADD_LIBRARY(lib-snowflakeid-shared SHARED $<TARGET_OBJECTS:lib-snowflakeid-obj>)
IF (NOT MSVC)
SET_TARGET_PROPERTIES(lib-snowflakeid-static PROPERTIES OUTPUT_NAME "snowflakeid")
ENDIF (NOT MSVC)
SET_TARGET_PROPERTIES(lib-snowflakeid-shared PROPERTIES OUTPUT_NAME "snowflakeid")
# Binary (Unit Tests)
ADD_EXECUTABLE(lib-snowflakeid-test $<TARGET_OBJECTS:lib-snowflakeid-obj> ${TEST_FILES})
# Install
IF (NOT MSVC)
INSTALL(TARGETS lib-snowflakeid-static ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
ENDIF (NOT MSVC)
INSTALL(TARGETS lib-snowflakeid-shared ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin)
INSTALL(FILES ${CMAKE_SOURCE_DIR}/inc/snowflakeid.h DESTINATION include)
# Current architecture/operating system
IF (NOT DEFINED ARCHITECTURE)
EXEC_PROGRAM("uname -m" OUTPUT_VARIABLE ARCHITECTURE)
ENDIF(NOT DEFINED ARCHITECTURE)
IF (APPLE)
EXEC_PROGRAM("sw_vers -productVersion" OUTPUT_VARIABLE OPERATING_SYSTEM_VERSION)
SET(OPERATING_SYSTEM "macos_${OPERATING_SYSTEM_VERSION}")
ELSE()
IF (WIN32)
SET(OPERATING_SYSTEM "windows")
ELSE()
EXEC_PROGRAM("awk -F= '$1==\"ID\" { print $2 ;}' /etc/os-release" OUTPUT_VARIABLE OPERATING_SYSTEM)
EXEC_PROGRAM("awk -F= '$1==\"VERSION_ID\" { print $2 ;}' /etc/os-release" OUTPUT_VARIABLE OPERATING_SYSTEM_VERSION)
STRING(CONCAT OPERATING_SYSTEM ${OPERATING_SYSTEM} "_" ${OPERATING_SYSTEM_VERSION})
STRING(REPLACE "\"" "" OPERATING_SYSTEM ${OPERATING_SYSTEM})
ENDIF(WIN32)
ENDIF(APPLE)
MESSAGE(STATUS "Architecture: ${ARCHITECTURE}")
MESSAGE(STATUS "Operating System: ${OPERATING_SYSTEM}")
# Packaging
SET(CPACK_PACKAGE_VERSION "${LIBSNOWFLAKEID_VERSION_STRING}")
SET(CPACK_PACKAGE_VERSION_MAJOR "${LIBSNOWFLAKEID_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${LIBSNOWFLAKEID_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${LIBSNOWFLAKEID_VERSION_PATCH}")
SET(CPACK_PACKAGE_NAME "libsnowflakeid")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/res/package-description.txt")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "A tiny, fast, and portable library to generate Snowflake IDs")
SET(PACKAGE_IGNORED_FILES "${CMAKE_CURRENT_BINARY_DIR}/;/.git/;.gitignore;~$;${CPACK_SOURCE_IGNORE_FILES}")
SET(CPACK_GENERATOR "TGZ")
SET(CPACK_ARCHIVE_FILE_NAME
"libsnowflakeid_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}\
_${OPERATING_SYSTEM}_${ARCHITECTURE}")
OPTION(BUILD_RPM "Build a RPM package" OFF)
IF (BUILD_RPM)
SET(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
SET(CPACK_RPM_PACKAGE_VENDOR "Thibault Meyer")
SET(CPACK_RPM_PACKAGE_LICENSE "MIT")
SET(CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
SET(CPACK_RPM_PACKAGE_URL "https://github.com/thibaultmeyer/libsnowflakeid")
SET(CPACK_RPM_FILE_NAME
"libsnowflakeid_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}\
_${OPERATING_SYSTEM}_${ARCHITECTURE}.rpm")
ENDIF (BUILD_RPM)
OPTION(BUILD_DEB "Build a DEB package" OFF)
IF (BUILD_DEB)
SET(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Thibault Meyer")
SET(CPACK_DEBIAN_PACKAGE_SECTION "devel")
SET(CPACK_DEBIAN_PACKAGE_HOMEPAGE "https://github.com/thibaultmeyer/libsnowflakeid")
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.4)")
SET(CPACK_DEBIAN_FILE_NAME
"libsnowflakeid_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}\
_${OPERATING_SYSTEM}_${ARCHITECTURE}.deb")
ENDIF (BUILD_DEB)
SET(CPACK_SOURCE_GENERATOR "TGZ")
SET(CPACK_SOURCE_PACKAGE_FILE_NAME ${PACKAGE_FILE_NAME})
SET(CPACK_SOURCE_IGNORE_FILES ${PACKAGE_IGNORED_FILES})
MESSAGE(STATUS "Generator: ${CPACK_GENERATOR}")
INCLUDE(CPack)