|
| 1 | +cmake_minimum_required(VERSION 3.16) |
| 2 | + |
| 3 | +set(CMAKE_CXX_STANDARD 17) |
| 4 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 5 | + |
| 6 | +find_package(Threads REQUIRED) |
| 7 | + |
| 8 | +if (NOT Ice_HOME) |
| 9 | + if (DEFINED ENV{ICE_HOME}) |
| 10 | + set(Ice_HOME $ENV{ICE_HOME}) |
| 11 | + else() |
| 12 | + message(FATAL_ERROR "Ice_HOME not set") |
| 13 | + endif() |
| 14 | +endif() |
| 15 | + |
| 16 | +if (NOT EXISTS ${Ice_HOME}) |
| 17 | + message(FATAL_ERROR "The specified Ice_HOME directory does not exist: ${Ice_HOME}") |
| 18 | +endif() |
| 19 | + |
| 20 | +find_program(Ice_SLICE2CPP_EXECUTABLE slice2cpp HINTS ${Ice_HOME}/cpp/bin PATH_SUFFIXES x64/Release x64/Debug) |
| 21 | + |
| 22 | +if (NOT Ice_SLICE2CPP_EXECUTABLE) |
| 23 | + message(FATAL_ERROR "slice2cpp executable not found") |
| 24 | +endif() |
| 25 | + |
| 26 | +if (NOT DEFINED Ice_SLICE_DIR) |
| 27 | + set(Ice_SLICE_DIR ${Ice_HOME}/slice CACHE PATH "Path to Ice slice files") |
| 28 | +endif() |
| 29 | + |
| 30 | +# TODO Use a variable |
| 31 | +if (WIN32) |
| 32 | +set(Ice_INCLUDE_DIRS ${Ice_HOME}/cpp/include ${Ice_HOME}/cpp/include/generated ${Ice_HOME}/cpp/include/generated/x64/Release) |
| 33 | +find_library(Ice_LIBRARY NAMES ice38a0 HINTS ${Ice_HOME}/cpp/lib PATH_SUFFIXES x64/Release) |
| 34 | +# set(Ice_LIBRARY ${ICE_LIBRARY} PARENT_SCOPE) |
| 35 | +elseif(APPLE) |
| 36 | +set(Ice_INCLUDE_DIRS ${Ice_HOME}/cpp/include ${Ice_HOME}/cpp/include/generated) |
| 37 | +set(Ice_LIBRARY ${Ice_HOME}/cpp/lib/libIce.dylib) |
| 38 | +# find_library(Ice_LIBRARY NAMES Ice.dylib HINTS ${Ice_HOME}/cpp/lib/) |
| 39 | +# set(Ice_LIBRARY ${ICE_LIBRARY} PARENT_SCOPE) |
| 40 | +else() |
| 41 | +set(Ice_INCLUDE_DIRS ${Ice_HOME}/cpp/include ${Ice_HOME}/cpp/include/generated) |
| 42 | +set(Ice_LIBRARY ${Ice_HOME}/cpp/lib/libIce.so) |
| 43 | +endif() |
| 44 | + |
| 45 | +add_library(Ice::Ice SHARED IMPORTED) |
| 46 | +set_target_properties(Ice::Ice PROPERTIES IMPORTED_IMPLIB ${Ice_LIBRARY}) |
| 47 | +set_target_properties(Ice::Ice PROPERTIES |
| 48 | + IMPORTED_LOCATION ${Ice_LIBRARY} |
| 49 | + INTERFACE_INCLUDE_DIRECTORIES "${Ice_INCLUDE_DIRS}" |
| 50 | +) |
| 51 | + |
| 52 | +# Function to generate C++ source files from Slice (.ice) files for a target using slice2cpp |
| 53 | +# The target must have the Slice files in its sources |
| 54 | +# The generated files are added to the target sources |
| 55 | +# Usage: |
| 56 | +# add_executable(a_target source1.cpp source2.ice source3.ice) |
| 57 | +# slice2cpp_generate(a_target) |
| 58 | +function(slice2cpp_generate TARGET) |
| 59 | + |
| 60 | + # Get the list of source files for the target |
| 61 | + get_target_property(sources ${TARGET} SOURCES) |
| 62 | + |
| 63 | + # Create a directory to store the generated files |
| 64 | + set(output_dir ${CMAKE_CURRENT_BINARY_DIR}/generated/${TARGET}) |
| 65 | + make_directory(${output_dir}) |
| 66 | + |
| 67 | + # Add the generated headers files to the target include directories |
| 68 | + target_include_directories(${TARGET} PRIVATE ${output_dir}) |
| 69 | + |
| 70 | + # Process each Slice (.ice) file in the source list |
| 71 | + # 1. Run the slice2cpp command to generate the header and source files |
| 72 | + # 2. Add the generated files to the target sources |
| 73 | + foreach(file IN LISTS sources) |
| 74 | + if(file MATCHES "\\.ice$") |
| 75 | + |
| 76 | + get_filename_component(slice_file_name ${file} NAME_WE) |
| 77 | + get_filename_component(slice_file_path ${file} ABSOLUTE) |
| 78 | + set(output_files ${output_dir}/${slice_file_name}.h ${output_dir}/${slice_file_name}.cpp) |
| 79 | + |
| 80 | + add_custom_command( |
| 81 | + OUTPUT ${output_files} |
| 82 | + COMMAND ${Ice_SLICE2CPP_EXECUTABLE} -I${Ice_SLICE_DIR} ${slice_file_path} --output-dir ${output_dir} |
| 83 | + DEPENDS ${slice_file_path} |
| 84 | + COMMENT "${Ice_SLICE2CPP_EXECUTABLE} ${file} -> ${slice_file_name}.h ${slice_file_name}.cpp" |
| 85 | + ) |
| 86 | + |
| 87 | + target_sources(${TARGET} PRIVATE ${output_files}) |
| 88 | + |
| 89 | + endif() |
| 90 | + endforeach() |
| 91 | + |
| 92 | +endfunction() |
0 commit comments