Skip to content

Commit 7c159e2

Browse files
cosmo0920edsiper
authored andcommitted
build: Add CMake related files
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent e8dc2ba commit 7c159e2

File tree

4 files changed

+483
-0
lines changed

4 files changed

+483
-0
lines changed

CMakeLists.txt

+341
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
cmake_minimum_required(VERSION 3.4)
2+
project(onigmo)
3+
4+
# Onigmo Version
5+
set(ONIGMO_VERSION_MAJOR 6)
6+
set(ONIGMO_VERSION_MINOR 2)
7+
set(ONIGMO_VERSION_PATCH 0)
8+
set(ONIGMO_VERSION_STR "${ONIGMO_VERSION_MAJOR}.${ONIGMO_VERSION_MINOR}.${ONIGMO_VERSION_PATCH}")
9+
10+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
11+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
12+
13+
# Define macro to identify Windows system (without Cygwin)
14+
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
15+
set(ONIGMO_SYSTEM_WINDOWS On)
16+
add_definitions(-DONIGMO_SYSTEM_WINDOWS)
17+
endif()
18+
19+
# Define macro to identify macOS system
20+
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
21+
set(ONIGMO_SYSTEM_MACOS On)
22+
add_definitions(-DONIGMO_SYSTEM_MACOS)
23+
endif()
24+
25+
# Define macro to identify Linux system
26+
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
27+
set(ONIGMO_SYSTEM_LINUX On)
28+
add_definitions(-DONIGMO_SYSTEM_LINUX)
29+
endif()
30+
31+
# Update CFLAGS
32+
if (MSVC)
33+
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
34+
add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
35+
36+
# Use custom CFLAGS for MSVC
37+
#
38+
# /Zi ...... Generate pdb files.
39+
# /MT ...... Static link C runtimes.
40+
# /wd4711 .. C4711 (function selected for inline expansion)
41+
# /wd4100 .. C4100 (unreferenced formal parameter)
42+
# /wd5045 .. C5045 (Spectre mitigation)
43+
#
44+
set(CMAKE_C_FLAGS "/DWIN32 /D_WINDOWS /DNDEBUG /O2 /Zi /wd4100 /wd4711 /wd5045")
45+
set(CMAKE_EXE_LINKER_FLAGS "/Debug /INCREMENTAL:NO")
46+
set(CMAKE_BUILD_TYPE None)
47+
48+
# We need this line in order to link libonigmo.lib statically.
49+
# Read onigmo/README for details.
50+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DEXPORT -DHAVE_CONFIG_H")
51+
# Use add_compile_options() to set /MT since Visual Studio
52+
# Generator does not notice /MT in CMAKE_C_FLAGS.
53+
add_compile_options(/MT)
54+
else()
55+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
56+
endif()
57+
58+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FLB_FILENAME__=__FILE__")
59+
60+
include(GNUInstallDirs)
61+
include(ExternalProject)
62+
include(CheckTypeSize)
63+
include(CheckIncludeFile)
64+
include(CheckSymbolExists)
65+
include(CheckCSourceCompiles)
66+
67+
# Output paths
68+
set(ONIGMO_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
69+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
70+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/lib")
71+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/library")
72+
73+
set(ONIGMO_OUT_NAME "onigmo")
74+
set(ONIGMO_PACKAGE_STRING "${ONIGMO_OUT_NAME} ${ONIGMO_VERSION_STR}")
75+
76+
# For config.h
77+
set(PACKAGE_NAME ${ONIGMO_OUT_NAME})
78+
set(PACKAGE_TARNAME ${ONIGMO_OUT_NAME})
79+
set(PACKAGE_VERSION ${ONIGMO_VERSION_STR})
80+
set(PACKAGE_STRING ${ONIGMO_PACKAGE_STRING})
81+
set(PACKAGE_BUGREPORT "")
82+
set(PACKAGE_URL "")
83+
84+
# Build Options
85+
option(USE_COMBINATION_EXPLOSION_CHECK "Enable combination explosion check" No)
86+
option(USE_CRNL_AS_LINE_TERMINATOR "Enable CR+NL as line terminator" No)
87+
option(ONIGMO_SHARED_LIB "Enable to build shared library" Yes)
88+
option(ONIGMO_CTESTS "Enable testing with ctest" Yes)
89+
option(ONIGMO_CTESTS_SAMPLE "Enable testing with ctest on sample" Yes)
90+
option(ONIGMO_PYTHON_TESTS "Enable testing with python tests" Yes)
91+
option(ONIGMO_MACOS_DEFAULTS "Build with predefined macOS settings" Yes)
92+
93+
if(USE_COMBINATION_EXPLOSION_CHECK)
94+
add_definitions(-DUSE_COMBINATION_EXPLOSION_CHECK=1)
95+
endif()
96+
97+
if(USE_CRNL_AS_LINE_TERMINATOR)
98+
add_definitions(-DUSE_CRNL_AS_LINE_TERMINATOR=1)
99+
endif()
100+
101+
# Search python executables
102+
if(ONIGMO_SYSTEM_WINDOWS)
103+
# On Windows, we need to prioritize for Python for Windows installer installed python.
104+
find_program(PYTHON_EXECUTABLE "python" "python3" "python2")
105+
else()
106+
find_program(PYTHON_EXECUTABLE "python3" "python" "python2")
107+
endif()
108+
message(STATUS "Using python executable is: ${PYTHON_EXECUTABLE}")
109+
110+
# Tweak build targets for macOS
111+
if(ONIGMO_SYSTEM_MACOS)
112+
include(cmake/macos-setup.cmake)
113+
endif()
114+
115+
check_type_size(int SIZEOF_INT)
116+
check_type_size(short SIZEOF_SHORT)
117+
check_type_size(long SIZEOF_LONG)
118+
check_type_size(void* SIZEOF_VOIDP)
119+
check_type_size("long long" SIZEOF_LONG_LONG)
120+
check_type_size("size_t" SIZEOF_SIZE_T)
121+
122+
# AC_HEADERS_STDC
123+
check_c_source_compiles("
124+
#include <stdlib.h>
125+
#include <stdarg.h>
126+
#include <string.h>
127+
#include <float.h>
128+
int main() {
129+
return 0;
130+
}" STDC_HEADERS)
131+
if(STDC_HEADERS)
132+
add_definitions(-DSTDC_HEADERS=1)
133+
endif()
134+
135+
check_include_file(dlfcn.h HAVE_DLFCN_H)
136+
check_include_file(inttypes.h HAVE_INTTYPES_H)
137+
check_include_file(memory.h HAVE_MEMORY_H)
138+
check_include_file(stdint.h HAVE_STDINT_H)
139+
check_include_file(stdlib.h HAVE_STDLIB_H)
140+
check_include_file(strings.h HAVE_STRINGS_H)
141+
check_include_file(string.h HAVE_STRING_H)
142+
check_include_file(sys/stat.h HAVE_SYS_STAT_H)
143+
144+
check_c_source_compiles("
145+
#include <sys/types.h>
146+
#include <sys/time.h>
147+
#include <time.h>
148+
int main() {
149+
struct tm *tm;
150+
return 0;
151+
}" TIME_WITH_SYS_TIME)
152+
if(TIME_WITH_SYS_TIME)
153+
add_definitions(-DTIME_WITH_SYS_TIME=1)
154+
endif()
155+
156+
check_include_file(sys/time.h HAVE_SYS_TIME_H)
157+
check_include_file(sys/times.h HAVE_SYS_TIMES_H)
158+
check_include_file(unistd.h HAVE_UNISTD_H)
159+
160+
check_symbol_exists(alloca alloca.h HAVE_ALLOCA_H)
161+
check_symbol_exists(memcmp string.h HAVE_MEMCMP)
162+
163+
164+
# Installation Directories
165+
# ========================
166+
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
167+
set(ONIGMO_INSTALL_BINDIR "bin")
168+
set(ONIGMO_INSTALL_LIBDIR "lib")
169+
set(ONIGMO_INSTALL_CONFDIR "conf")
170+
set(ONIGMO_INSTALL_DATADIR "share")
171+
set(ONIGMO_INSTALL_INCLUDEDIR "include")
172+
else()
173+
set(ONIGMO_INSTALL_BINDIR ${CMAKE_INSTALL_FULL_BINDIR})
174+
set(ONIGMO_INSTALL_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
175+
set(ONIGMO_INSTALL_CONFDIR "${CMAKE_INSTALL_SYSCONFDIR}/${ONIGMO_OUT_NAME}/")
176+
set(ONIGMO_INSTALL_DATADIR "${CMAKE_INSTALL_PREFIX}/share")
177+
set(ONIGMO_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
178+
endif()
179+
180+
configure_file(${ONIGMO_ROOT}/config.h.cmake ${ONIGMO_ROOT}/config.h)
181+
configure_file(${ONIGMO_ROOT}/onigmo.pc.cmake ${ONIGMO_ROOT}/onigmo.pc @ONLY)
182+
183+
include_directories(${ONIGMO_ROOT}
184+
${ONIGMO_ROOT}/enc/unicode/
185+
${ONIGMO_ROOT}/enc/
186+
)
187+
188+
# Source
189+
set(src
190+
${src}
191+
regerror.c
192+
regparse.c
193+
regext.c
194+
regcomp.c
195+
regexec.c
196+
reggnu.c
197+
regenc.c
198+
regsyntax.c
199+
regtrav.c
200+
regversion.c
201+
st.c
202+
regposix.c
203+
regposerr.c
204+
enc/unicode.c
205+
enc/ascii.c
206+
enc/utf_8.c
207+
enc/utf_16be.c
208+
enc/utf_16le.c
209+
enc/utf_32be.c
210+
enc/utf_32le.c
211+
enc/euc_jp.c
212+
enc/shift_jis.c
213+
enc/windows_31j.c
214+
enc/jis/props.kwd
215+
enc/iso_8859.h
216+
enc/iso_8859_1.c
217+
enc/iso_8859_2.c
218+
enc/iso_8859_3.c
219+
enc/iso_8859_4.c
220+
enc/iso_8859_5.c
221+
enc/iso_8859_6.c
222+
enc/iso_8859_7.c
223+
enc/iso_8859_8.c
224+
enc/iso_8859_9.c
225+
enc/iso_8859_10.c
226+
enc/iso_8859_11.c
227+
enc/iso_8859_13.c
228+
enc/iso_8859_14.c
229+
enc/iso_8859_15.c
230+
enc/iso_8859_16.c
231+
enc/euc_tw.c
232+
enc/euc_kr.c
233+
enc/big5.c
234+
enc/gb18030.c
235+
enc/koi8_r.c
236+
enc/koi8_u.c
237+
enc/windows_1250.c
238+
enc/windows_1251.c
239+
enc/windows_1252.c
240+
enc/windows_1253.c
241+
enc/windows_1254.c
242+
enc/windows_1257.c
243+
)
244+
245+
# Static Library
246+
add_library(onigmo-static STATIC ${src})
247+
248+
# Shared Library
249+
if(ONIGMO_SHARED_LIB)
250+
add_library(onigmo-shared SHARED ${src})
251+
set_target_properties(onigmo-shared
252+
PROPERTIES OUTPUT_NAME onigmo
253+
WINDOWS_EXPORT_ALL_SYMBOLS ON)
254+
255+
if (MSVC)
256+
set_target_properties(onigmo-shared
257+
PROPERTIES PDB_NAME onigmo.dll)
258+
target_link_options(onigmo-shared
259+
PUBLIC /pdb:$<TARGET_PDB_FILE:onigmo-shared>
260+
PRIVATE /LTCG)
261+
endif()
262+
263+
# Library install routines
264+
install(TARGETS onigmo-shared
265+
LIBRARY DESTINATION ${ONIGMO_INSTALL_LIBDIR}
266+
COMPONENT library
267+
RUNTIME DESTINATION ${ONIGMO_INSTALL_BINDIR})
268+
endif()
269+
270+
# Test Source
271+
272+
if(ONIGMO_CTESTS)
273+
enable_testing()
274+
if(ONIGMO_SYSTEM_WINDOWS)
275+
add_executable(testc win32/testc.c)
276+
target_compile_options(testc PUBLIC -DONIG_EXTERN=extern)
277+
else()
278+
add_executable(testc testc.c)
279+
endif()
280+
add_test(NAME testc COMMAND testc)
281+
target_link_libraries(testc onigmo-static)
282+
283+
if(ONIGMO_SYSTEM_WINDOWS)
284+
add_executable(testp win32/testc.c)
285+
else()
286+
add_executable(testp testc.c)
287+
endif()
288+
target_compile_options(testp PUBLIC -DPOSIX_TEST)
289+
add_test(NAME testp COMMAND testp)
290+
target_link_libraries(testp onigmo-static)
291+
292+
add_executable(testu testu.c)
293+
add_test(NAME testu COMMAND testu)
294+
target_link_libraries(testu onigmo-static)
295+
296+
add_executable(test_enc_utf8 test_enc_utf8.c)
297+
add_test(NAME test_enc_utf8 COMMAND test_enc_utf8)
298+
target_link_libraries(test_enc_utf8 onigmo-static)
299+
endif()
300+
301+
if(ONIGMO_CTESTS_SAMPLE)
302+
enable_testing()
303+
macro(SAMPLE_TEST_ADD name)
304+
add_executable(${name} sample/${name}.c)
305+
add_test(NAME ${name} COMMAND ${name})
306+
target_link_libraries(${name} onigmo-static)
307+
endmacro()
308+
SAMPLE_TEST_ADD(encode)
309+
SAMPLE_TEST_ADD(listcap)
310+
SAMPLE_TEST_ADD(names)
311+
SAMPLE_TEST_ADD(posix)
312+
SAMPLE_TEST_ADD(simple)
313+
SAMPLE_TEST_ADD(sql)
314+
SAMPLE_TEST_ADD(syntax)
315+
SAMPLE_TEST_ADD(scan)
316+
SAMPLE_TEST_ADD(crnl)
317+
endif()
318+
319+
if(PYTHON_EXECUTABLE AND ONIGMO_PYTHON_TESTS AND ONIGMO_SHARED_LIB)
320+
enable_testing()
321+
set(TEST_WORK_DIR "")
322+
if (ONIGMO_SYSTEM_LINUX)
323+
set(TEST_LIBRARY_PATH_ENV "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/lib")
324+
elseif(ONIGMO_SYSTEM_MACOS)
325+
set(TEST_LIBRARY_PATH_ENV "DYLD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/lib")
326+
elseif(ONIGMO_SYSTEM_WINDOWS)
327+
set(TEST_WORK_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
328+
endif()
329+
macro(PYTHON_TEST_ADD name encoding)
330+
add_test(NAME ${name} COMMAND ${PYTHON_EXECUTABLE} ${ONIGMO_ROOT}/testpy.py ${encoding}
331+
WORKING_DIRECTORY ${TEST_WORK_DIR})
332+
set_property(TEST ${name} PROPERTY ENVIRONMENT ${TEST_LIBRARY_PATH_ENV})
333+
endmacro()
334+
PYTHON_TEST_ADD(python-euc-jp EUC-JP)
335+
PYTHON_TEST_ADD(python-sjis SJIS)
336+
PYTHON_TEST_ADD(python-utf8 UTF-8)
337+
PYTHON_TEST_ADD(python-utf16le UTF-16LE)
338+
PYTHON_TEST_ADD(python-utf16be UTF-16BE)
339+
PYTHON_TEST_ADD(python-utf32le UTF-32LE)
340+
PYTHON_TEST_ADD(python-utf32be UTF-32BE)
341+
endif()

cmake/macos-setup.cmake

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Custom build settings for macOS
2+
#
3+
# pytest are not supported on macOS yet. This file tweaks
4+
# the build flags so that we can execute tests for onigmo on it.
5+
6+
if(ONIGMO_MACOS_DEFAULTS)
7+
message(STATUS "Overriding setttings with macos-setup.cmake")
8+
set(ONIGMO_PYTHON_TESTS No)
9+
endif()

0 commit comments

Comments
 (0)