Skip to content

Commit 561f229

Browse files
authored
Initial CMake build support (#252)
1 parent cfd9a37 commit 561f229

File tree

9 files changed

+224
-8
lines changed

9 files changed

+224
-8
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ jobs:
9191
working_directory: ice-demos/cpp
9292
msbuild_project: msbuild/ice.proj
9393

94+
- name: Build C++ Demos on ${{ matrix.os }} with CMake
95+
timeout-minutes: 5
96+
working-directory: ice-demos/cpp
97+
run:
98+
find . -name CMakeLists.txt -execdir cmake -B build -S . \; -execdir cmake --build build --config Release \;
99+
94100
- name: Build C# Demos on ${{ matrix.os }}
95101
timeout-minutes: 20
96102
working-directory: ice-demos/csharp

.vscode/settings.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44
"Make.*": "makefile"
55
},
66
"C_Cpp.default.cppStandard": "c++17",
7-
"gradle.nestedProjects": true
7+
"gradle.nestedProjects": true,
8+
"cmake.sourceDirectory":[
9+
"${workspaceFolder}/cpp/Ice/greeter",
10+
"${workspaceFolder}/cpp/Ice/greeterAsync",
11+
"${workspaceFolder}/cpp/Ice/secure",
12+
],
13+
"cmake.configureSettings": {
14+
"Ice_HOME": "${workspaceFolder}/../ice",
15+
}
816
}

cpp/Ice/greeter/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(greeter CXX)
4+
5+
include(../../cmake/common.cmake)
6+
7+
add_executable(client Client.cpp Greeter.ice)
8+
slice2cpp_generate(client)
9+
target_link_libraries(client Ice::Ice)
10+
11+
add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
12+
slice2cpp_generate(server)
13+
target_link_libraries(server Ice::Ice)

cpp/Ice/greeter/README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,37 @@
22

33
The Greeter demo illustrates how to send a request and wait for the response.
44

5+
To build the demo run:
6+
7+
```shell
8+
cmake -B build
9+
cmake --build build --config Release
10+
```
11+
512
To run the demo, first start the server:
13+
14+
**Linux/macOS:**
15+
16+
```shell
17+
./build/server
618
```
7-
server
19+
20+
**Windows:**
21+
22+
```shell
23+
.\build\Release\server
824
```
925

1026
In a separate window, start the client:
27+
28+
**Linux/macOS:**
29+
30+
```shell
31+
./build/client
1132
```
12-
client
33+
34+
**Windows:**
35+
36+
```shell
37+
.\build\Release\client
1338
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(greeter_async CXX)
4+
5+
include(../../cmake/common.cmake)
6+
7+
add_executable(client Client.cpp Greeter.ice)
8+
slice2cpp_generate(client)
9+
target_link_libraries(client Ice::Ice)
10+
11+
add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
12+
slice2cpp_generate(server)
13+
target_link_libraries(server Ice::Ice)

cpp/Ice/greeterAsync/README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@
22

33
The Greeter demo illustrates how to send a request and wait for the response.
44

5+
To build the demo run:
6+
7+
```shell
8+
cmake -B build
9+
cmake --build build --config Release
10+
```
11+
512
To run the demo, first start the server:
13+
14+
**Linux/macOS:**
15+
16+
```shell
17+
./build/server
618
```
7-
server
19+
20+
**Windows:**
21+
22+
```shell
23+
.\build\Release\server
824
```
925

1026
In a separate window, start the client:
27+
28+
**Linux/macOS:**
29+
30+
```shell
31+
./build/client
1132
```
12-
client
13-
```
33+
34+
**Windows:**
35+
36+
```shell
37+
./build/Release/client

cpp/Ice/secure/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(secure CXX)
4+
5+
include(../../cmake/common.cmake)
6+
7+
add_executable(client Client.cpp Greeter.ice)
8+
slice2cpp_generate(client)
9+
target_link_libraries(client Ice::Ice)
10+
11+
add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
12+
slice2cpp_generate(server)
13+
target_link_libraries(server Ice::Ice)

cpp/Ice/secure/README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@
33
This demo shows how to configure client and server applications to communicate over
44
secure connections.
55

6+
To build the demo run:
7+
8+
```shell
9+
cmake -B build
10+
cmake --build build --config Release
11+
```
12+
613
To run the demo, first start the server:
714

15+
**Linux/macOS:**
16+
817
```shell
9-
server
18+
./build/server
19+
```
20+
21+
**Windows:**
22+
23+
```shell
24+
.\build\Release\server
1025
```
1126

1227
In a separate window, start the client:
1328

29+
**Linux/macOS:**
30+
1431
```shell
15-
client
32+
./build/client
1633
```
34+
35+
**Windows:**
36+
37+
```shell
38+
./build/Release/client

cpp/cmake/common.cmake

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)