|
1 | | -project(Proton CXX) |
| 1 | +project(Proton LANGUAGES CXX) |
2 | 2 |
|
3 | | -set(PROTON_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/csrc) |
4 | | -set(PROTON_EXTERN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/extern) |
5 | | -file(GLOB_RECURSE PROTON_SRC ${PROTON_SRC_DIR}/lib/*.cpp) |
6 | | -add_library(proton SHARED ${PROTON_SRC} ${PROTON_SRC_DIR}/${PROJECT_NAME}.cpp) |
| 3 | +set(PROTON_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/csrc") |
7 | 4 |
|
| 5 | +# ============ Check for includes ============= |
8 | 6 | if(NOT CUPTI_INCLUDE_DIR) |
9 | 7 | message(FATAL_ERROR "CUPTI include directory not defined") |
10 | 8 | endif() |
11 | 9 | if(NOT ROCTRACER_INCLUDE_DIR) |
12 | 10 | message(FATAL_ERROR "ROCTRACER include directory not defined") |
13 | 11 | endif() |
14 | | -if (NOT JSON_INCLUDE_DIR) |
| 12 | +if(NOT JSON_INCLUDE_DIR) |
15 | 13 | message(FATAL_ERROR "JSON include directory not defined") |
16 | 14 | endif() |
17 | 15 |
|
18 | | -include_directories(${JSON_INCLUDE_DIR}) |
19 | | -include_directories(${PROTON_SRC_DIR}/include) |
20 | | -include_directories(${PROTON_EXTERN_DIR}) |
21 | | - |
| 16 | +# ============ Dependencies ============= |
22 | 17 | find_package(Python3 REQUIRED Interpreter Development.Module) |
23 | 18 | find_package(pybind11 CONFIG REQUIRED HINTS "${Python3_SITELIB}") |
24 | 19 |
|
25 | | -# Check if the platform is MacOS |
| 20 | +# ============ Define a GLOBAL property to store object-libraries ============ |
| 21 | +set_property(GLOBAL PROPERTY PROTON_LIBS "") |
| 22 | + |
| 23 | +# ============ Define a function to create object libraries ============ |
| 24 | +function(add_proton_library name) |
| 25 | + add_library(${name} OBJECT ${ARGN}) |
| 26 | + |
| 27 | + target_link_libraries(${name} PRIVATE Python3::Module pybind11::headers) |
| 28 | + |
| 29 | + target_include_directories(${name} |
| 30 | + PRIVATE |
| 31 | + "${CUPTI_INCLUDE_DIR}" |
| 32 | + "${ROCTRACER_INCLUDE_DIR}" |
| 33 | + "${JSON_INCLUDE_DIR}" |
| 34 | + "${PROTON_SRC_DIR}/include" |
| 35 | + ) |
| 36 | + |
| 37 | + # If HIP is AMD-based |
| 38 | + target_compile_definitions(${name} PRIVATE __HIP_PLATFORM_AMD__) |
| 39 | + |
| 40 | + # Append this library name to the GLOBAL property "PROTON_LIBS" |
| 41 | + set_property(GLOBAL APPEND PROPERTY PROTON_LIBS ${name}) |
| 42 | +endfunction() |
| 43 | + |
| 44 | +# ============ Add subdirectory with actual code that calls add_proton_library ============ |
| 45 | +add_subdirectory("${PROTON_SRC_DIR}") |
| 46 | + |
| 47 | +# ============ Possibly handle macOS specifics ============ |
26 | 48 | if(APPLE) |
27 | 49 | set(CMAKE_SHARED_LIBRARY_SUFFIX ".so") |
28 | 50 | # Other platforms build with -flto, but we found that this adds significant overhead to our macos CI without providing a major benefit. |
29 | 51 | set(PROTON_PYTHON_LDFLAGS "-undefined dynamic_lookup") |
30 | 52 | endif() |
31 | 53 |
|
32 | | -include_directories(${CUPTI_INCLUDE_DIR}) |
33 | | -include_directories(SYSTEM ${ROCTRACER_INCLUDE_DIR}) |
34 | | -target_compile_definitions(proton PRIVATE __HIP_PLATFORM_AMD__) |
| 54 | +# ============ Collect all object libraries from property and build final shared lib ============ |
| 55 | +get_property(_proton_obj_libs GLOBAL PROPERTY PROTON_LIBS) |
| 56 | + |
| 57 | +if(NOT _proton_obj_libs) |
| 58 | + message(WARNING "No object libraries were defined in 'PROTON_LIBS'!") |
| 59 | +endif() |
| 60 | + |
| 61 | +set(_proton_obj_sources "") |
| 62 | +foreach(_lib IN LISTS _proton_obj_libs) |
| 63 | + list(APPEND _proton_obj_sources $<TARGET_OBJECTS:${_lib}>) |
| 64 | + message(STATUS "Collecting object files from ${_lib}") |
| 65 | +endforeach() |
| 66 | + |
| 67 | +add_library(proton SHARED ${_proton_obj_sources}) |
35 | 68 |
|
36 | | -target_link_libraries(proton PRIVATE Python3::Module pybind11::headers) |
37 | | -target_link_options(proton PRIVATE ${PROTON_PYTHON_LDFLAGS}) |
| 69 | +target_link_libraries(proton PRIVATE Python3::Module) |
| 70 | +# Apply any macOS linker flags or extra link options |
| 71 | +if(PROTON_PYTHON_LDFLAGS) |
| 72 | + target_link_options(proton PRIVATE ${PROTON_PYTHON_LDFLAGS}) |
| 73 | +endif() |
0 commit comments