1+ # Modified from https://github.com/pybind/scikit_build_example/blob/master/CMakeLists.txt
2+ cmake_minimum_required (VERSION 3.15...3.27 )
3+
4+ set (CMAKE_C_COMPILER "gcc" )
5+ set (CMAKE_CXX_COMPILER "g++" )
6+ set (CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>" )
7+ set (CMAKE_CXX_COMPILE_OBJECT "<CMAKE_CXX_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -c <SOURCE>" )
8+ # enable -fPIC
9+ set (CMAKE_POSITION_INDEPENDENT_CODE ON )
10+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS } -fPIC" )
11+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS } -fPIC" )
12+
13+ project (
14+ ${SKBUILD_PROJECT_NAME}
15+ VERSION ${SKBUILD_PROJECT_VERSION}
16+ LANGUAGES C CXX )
17+
18+ # use customized cmake module
19+ set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH } "${CMAKE_SOURCE_DIR } /../cmake/Modules/" )
20+
21+ # set the logging level to DEBUG by default
22+ set (LOG_LEVEL DEBUG CACHE STRING "change the logging level" )
23+
24+ string (TOLOWER "${LOG_LEVEL} " LOG_LEVEL_LOWER)
25+
26+ if (LOG_LEVEL_LOWER STREQUAL "vvverbose" )
27+ add_compile_definitions (LOGLEVEL=3 )
28+ elseif (LOG_LEVEL_LOWER STREQUAL "vverbose" )
29+ add_compile_definitions (LOGLEVEL=4 )
30+ elseif (LOG_LEVEL_LOWER STREQUAL "verbose" )
31+ add_compile_definitions (LOGLEVEL=5 )
32+ elseif (LOG_LEVEL_LOWER STREQUAL "debug" )
33+ add_compile_definitions (LOGLEVEL=6 )
34+ elseif (LOG_LEVEL_LOWER STREQUAL "info" )
35+ add_compile_definitions (LOGLEVEL=7 )
36+ elseif (LOG_LEVEL_LOWER STREQUAL "warn" )
37+ add_compile_definitions (LOGLEVEL=8 )
38+ elseif (LOG_LEVEL_LOWER STREQUAL "error" )
39+ add_compile_definitions (LOGLEVEL=9 )
40+
41+ # default none is info
42+ elseif (LOG_LEVEL_LOWER STREQUAL "none" )
43+ add_compile_definitions (LOGLEVEL=7 )
44+ else ()
45+ message (WARNING "unknown log level ${LOG_LEVEL} , use INFO as default" )
46+ add_compile_definitions (LOGLEVEL=7 )
47+ endif ()
48+
49+ option (ENABLE_LRB "enable LRB" ON )
50+ option (ENABLE_GLCACHE "enable GLCache" ON )
51+ option (ENABLE_3L_CACHE "enable 3LCache" ON )
52+ add_compile_definitions (ENABLE_GLCACHE=1 )
53+ add_compile_definitions (ENABLE_LRB=1 )
54+ add_compile_definitions (ENABLE_3L_CACHE=1 )
55+
56+ # find python and pybind11
57+ find_package (Python REQUIRED COMPONENTS Interpreter Development.Module )
58+ find_package (pybind11 CONFIG REQUIRED )
59+
60+ # find glib
61+ find_package (PkgConfig REQUIRED )
62+ pkg_check_modules (GLIB2 REQUIRED glib-2.0 )
63+
64+ # ------------------------------------------------------------
65+ include_directories (
66+ ${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/include
67+ ${GLIB2_INCLUDE_DIRS}
68+ )
69+
70+ file (GLOB UTILS_SOURCES
71+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/utils/*.c"
72+ )
73+
74+ add_library (utils STATIC ${UTILS_SOURCES} )
75+ set_target_properties (utils PROPERTIES
76+ LANGUAGE C
77+ POSITION_INDEPENDENT_CODE ON
78+ )
79+ target_compile_options (utils PRIVATE -fPIC )
80+ target_link_libraries (utils PRIVATE ${GLIB2_LIBRARIES} )
81+
82+ file (GLOB_RECURSE CACHE_SOURCES
83+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/*.c"
84+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/eviction/*.c"
85+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/admission/*.c"
86+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/prefetch/*.c"
87+ )
88+
89+ # Try to find XGBoost and LightGBM (optional)
90+ find_package (xgboost QUIET )
91+ if (XGBOOST_FOUND)
92+ message (STATUS "XGBoost found, enabling GLCache" )
93+ include_directories (${XGBOOST_INCLUDE_DIR} )
94+ set (XGBOOST_LIBRARIES xgboost::xgboost)
95+ add_compile_definitions (ENABLE_XGBOOST=1 )
96+ else ()
97+ message (WARNING "XGBoost not found, disabling GLCache" )
98+ add_compile_definitions (ENABLE_XGBOOST=0 )
99+ set (XGBOOST_LIBRARIES "" )
100+ endif ()
101+
102+ find_path (LIGHTGBM_PATH LightGBM QUIET )
103+ find_library (LIGHTGBM_LIB _lightgbm QUIET )
104+ if (LIGHTGBM_PATH AND LIGHTGBM_LIB)
105+ message (STATUS "LightGBM found, enabling LRB and 3LCache" )
106+ include_directories (${LIGHTGBM_PATH} )
107+ set (LIGHTGBM_LIBRARIES ${LIGHTGBM_LIB} )
108+ add_compile_definitions (ENABLE_LIGHTGBM=1 )
109+ else ()
110+ message (WARNING "LightGBM not found, disabling LRB and 3LCache" )
111+ add_compile_definitions (ENABLE_LIGHTGBM=0 )
112+ set (LIGHTGBM_LIBRARIES "" )
113+ endif ()
114+
115+ # Only include sources if dependencies are available
116+ if (ENABLE_XGBOOST)
117+ file (GLOB_RECURSE LRB_SOURCES
118+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/eviction/LRB/*.cpp"
119+ )
120+ file (GLOB_RECURSE 3L_CACHE_SOURCES
121+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/eviction/3LCache/*.cpp"
122+ )
123+ else ()
124+ set (LRB_SOURCES "" )
125+ set (3L_CACHE_SOURCES "" )
126+ endif ()
127+
128+ file (GLOB_RECURSE CACHE_CPP_SOURCES
129+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/eviction/LHD/*"
130+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/cache/eviction/cpp/*"
131+ )
132+
133+ file (GLOB_RECURSE DS_SOURCES
134+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/dataStructure/*.c"
135+ )
136+
137+ file (GLOB_RECURSE READER_SOURCES
138+ "${CMAKE_CURRENT_SOURCE_DIR } /../libCacheSim/traceReader/*.c"
139+ )
140+
141+ # ------------------------------------------------------------
142+
143+ add_library (cachesim_c STATIC
144+ ${UTILS_SOURCES}
145+ ${CACHE_SOURCES}
146+ ${LRB_SOURCES}
147+ ${3L_CACHE_SOURCES}
148+ ${DS_SOURCES}
149+ ${READER_SOURCES}
150+ )
151+
152+ set_target_properties (cachesim_c PROPERTIES
153+ LANGUAGE C
154+ POSITION_INDEPENDENT_CODE ON
155+ )
156+
157+ target_compile_options (cachesim_c PRIVATE -fPIC )
158+ target_link_libraries (cachesim_c PRIVATE ${GLIB2_LIBRARIES} )
159+
160+ add_library (cachesim_cpp STATIC ${CACHE_CPP_SOURCES} )
161+ set_target_properties (cachesim_cpp PROPERTIES
162+ LANGUAGE CXX
163+ POSITION_INDEPENDENT_CODE ON
164+ )
165+ target_compile_options (cachesim_cpp PRIVATE -fPIC )
166+ target_link_libraries (cachesim_cpp PRIVATE ${GLIB2_LIBRARIES} )
167+
168+ # create python module
169+ python_add_library (_libcachesim MODULE
170+ src/pylibcachesim.cpp
171+ WITH_SOABI
172+ )
173+
174+ set_target_properties (_libcachesim PROPERTIES
175+ POSITION_INDEPENDENT_CODE ON
176+ INSTALL_RPATH_USE_LINK_PATH TRUE
177+ BUILD_WITH_INSTALL_RPATH TRUE
178+ INSTALL_RPATH "$ORIGIN"
179+ )
180+
181+ target_compile_options (_libcachesim PRIVATE -fPIC )
182+ target_compile_definitions (_libcachesim PRIVATE VERSION_INFO=${PROJECT_VERSION} )
183+
184+ target_link_libraries (_libcachesim PRIVATE
185+ cachesim_c
186+ cachesim_cpp
187+ pybind11::headers
188+ ${GLIB2_LIBRARIES}
189+ ${XGBOOST_LIBRARIES}
190+ ${LIGHTGBM_LIBRARIES}
191+ -Wl,--no-as-needed -ldl
192+ )
193+
194+ # install to wheel directory
195+ install (TARGETS _libcachesim cachesim_c
196+ LIBRARY DESTINATION libcachesim
197+ )
0 commit comments