Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial CMake build support #252

Merged
merged 23 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ jobs:
working_directory: ice-demos/cpp
msbuild_project: msbuild/ice.proj

- name: Build C++ Demos on ${{ matrix.os }} with CMake
timeout-minutes: 5
working-directory: ice-demos/cpp
run:
find . -name CMakeLists.txt -execdir cmake -B build -S . \; -execdir cmake --build build --config Release \;

- name: Build C# Demos on ${{ matrix.os }}
timeout-minutes: 20
working-directory: ice-demos/csharp
Expand Down
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@
"Make.*": "makefile"
},
"C_Cpp.default.cppStandard": "c++17",
"gradle.nestedProjects": true
"gradle.nestedProjects": true,
"cmake.sourceDirectory":[
"${workspaceFolder}/cpp/Ice/greeter",
"${workspaceFolder}/cpp/Ice/greeterAsync",
"${workspaceFolder}/cpp/Ice/secure",
],
"cmake.configureSettings": {
"Ice_HOME": "${workspaceFolder}/../ice",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sets the cmake variable Ice_HOME to our usual "expected" location, adjacent to the ice-demos dir. This way you don't need to open visual studio code with the correct environment.

}
}
13 changes: 13 additions & 0 deletions cpp/Ice/greeter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.16)

project(greeter CXX)

include(../../cmake/common.cmake)

add_executable(client Client.cpp Greeter.ice)
slice2cpp_generate(client)
target_link_libraries(client Ice::Ice)

add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
slice2cpp_generate(server)
target_link_libraries(server Ice::Ice)
29 changes: 27 additions & 2 deletions cpp/Ice/greeter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@

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

To build the demo run:

```shell
cmake -B build
cmake --build build --config Release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is Debug? Should we just show the default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I read cmake doesn't have a default and it's all about the generator's default which apparently in several cases is debug.

```

To run the demo, first start the server:

**Linux/macOS:**

```shell
./build/server
```
server

**Windows:**

```shell
.\build\Release\server
```

In a separate window, start the client:

**Linux/macOS:**

```shell
./build/client
```
client

**Windows:**

```shell
./build/Release/client
externl marked this conversation as resolved.
Show resolved Hide resolved
```
13 changes: 13 additions & 0 deletions cpp/Ice/greeterAsync/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.16)

project(greeter_async CXX)

include(../../cmake/common.cmake)

add_executable(client Client.cpp Greeter.ice)
slice2cpp_generate(client)
target_link_libraries(client Ice::Ice)

add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
slice2cpp_generate(server)
target_link_libraries(server Ice::Ice)
30 changes: 27 additions & 3 deletions cpp/Ice/greeterAsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,36 @@

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

To build the demo run:

```shell
cmake -B build
cmake --build build --config Release
```

To run the demo, first start the server:

**Linux/macOS:**

```shell
./build/server
```
server

**Windows:**

```shell
.\build\Release\server
```

In a separate window, start the client:

**Linux/macOS:**

```shell
./build/client
```
client
```

**Windows:**

```shell
./build/Release/client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
./build/Release/client
.\build\Release\client

13 changes: 13 additions & 0 deletions cpp/Ice/secure/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.16)

project(secure CXX)

include(../../cmake/common.cmake)

add_executable(client Client.cpp Greeter.ice)
slice2cpp_generate(client)
target_link_libraries(client Ice::Ice)

add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
slice2cpp_generate(server)
target_link_libraries(server Ice::Ice)
26 changes: 24 additions & 2 deletions cpp/Ice/secure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@
This demo shows how to configure client and server applications to communicate over
secure connections.

To build the demo run:

```shell
cmake -B build
cmake --build build --config Release
```

To run the demo, first start the server:

**Linux/macOS:**

```shell
server
./build/server
```

**Windows:**

```shell
.\build\Release\server
```

In a separate window, start the client:

**Linux/macOS:**

```shell
client
./build/client
```

**Windows:**

```shell
./build/Release/client
92 changes: 92 additions & 0 deletions cpp/cmake/common.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Threads REQUIRED)

if (NOT Ice_HOME)
externl marked this conversation as resolved.
Show resolved Hide resolved
if (DEFINED ENV{ICE_HOME})
set(Ice_HOME $ENV{ICE_HOME})
else()
message(FATAL_ERROR "Ice_HOME not set")
externl marked this conversation as resolved.
Show resolved Hide resolved
endif()
endif()

if (NOT EXISTS ${Ice_HOME})
message(FATAL_ERROR "The specified Ice_HOME directory does not exist: ${Ice_HOME}")
endif()

find_program(Ice_SLICE2CPP_EXECUTABLE slice2cpp HINTS ${Ice_HOME}/cpp/bin PATH_SUFFIXES x64/Release x64/Debug)
externl marked this conversation as resolved.
Show resolved Hide resolved

if (NOT Ice_SLICE2CPP_EXECUTABLE)
message(FATAL_ERROR "slice2cpp executable not found")
endif()

if (NOT DEFINED Ice_SLICE_DIR)
set(Ice_SLICE_DIR ${Ice_HOME}/slice CACHE PATH "Path to Ice slice files")
endif()

# TODO Use a variable
if (WIN32)
externl marked this conversation as resolved.
Show resolved Hide resolved
set(Ice_INCLUDE_DIRS ${Ice_HOME}/cpp/include ${Ice_HOME}/cpp/include/generated ${Ice_HOME}/cpp/include/generated/x64/Release)
find_library(Ice_LIBRARY NAMES ice38a0 HINTS ${Ice_HOME}/cpp/lib PATH_SUFFIXES x64/Release)
# set(Ice_LIBRARY ${ICE_LIBRARY} PARENT_SCOPE)
elseif(APPLE)
set(Ice_INCLUDE_DIRS ${Ice_HOME}/cpp/include ${Ice_HOME}/cpp/include/generated)
set(Ice_LIBRARY ${Ice_HOME}/cpp/lib/libIce.dylib)
# find_library(Ice_LIBRARY NAMES Ice.dylib HINTS ${Ice_HOME}/cpp/lib/)
# set(Ice_LIBRARY ${ICE_LIBRARY} PARENT_SCOPE)
else()
set(Ice_INCLUDE_DIRS ${Ice_HOME}/cpp/include ${Ice_HOME}/cpp/include/generated)
set(Ice_LIBRARY ${Ice_HOME}/cpp/lib/libIce.so)
endif()

add_library(Ice::Ice SHARED IMPORTED)
set_target_properties(Ice::Ice PROPERTIES IMPORTED_IMPLIB ${Ice_LIBRARY})
set_target_properties(Ice::Ice PROPERTIES
IMPORTED_LOCATION ${Ice_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES "${Ice_INCLUDE_DIRS}"
)

# Function to generate C++ source files from Slice (.ice) files for a target using slice2cpp
# The target must have the Slice files in its sources
# The generated files are added to the target sources
# Usage:
# add_executable(a_target source1.cpp source2.ice source3.ice)
# slice2cpp_generate(a_target)
function(slice2cpp_generate TARGET)

# Get the list of source files for the target
get_target_property(sources ${TARGET} SOURCES)

# Create a directory to store the generated files
set(output_dir ${CMAKE_CURRENT_BINARY_DIR}/generated/${TARGET})
make_directory(${output_dir})

# Add the generated headers files to the target include directories
target_include_directories(${TARGET} PRIVATE ${output_dir})

# Process each Slice (.ice) file in the source list
# 1. Run the slice2cpp command to generate the header and source files
# 2. Add the generated files to the target sources
foreach(file IN LISTS sources)
externl marked this conversation as resolved.
Show resolved Hide resolved
if(file MATCHES "\\.ice$")

get_filename_component(slice_file_name ${file} NAME_WE)
get_filename_component(slice_file_path ${file} ABSOLUTE)
set(output_files ${output_dir}/${slice_file_name}.h ${output_dir}/${slice_file_name}.cpp)

add_custom_command(
OUTPUT ${output_files}
COMMAND ${Ice_SLICE2CPP_EXECUTABLE} -I${Ice_SLICE_DIR} ${slice_file_path} --output-dir ${output_dir}
DEPENDS ${slice_file_path}
COMMENT "${Ice_SLICE2CPP_EXECUTABLE} ${file} -> ${slice_file_name}.h ${slice_file_name}.cpp"
)

target_sources(${TARGET} PRIVATE ${output_files})

endif()
endforeach()

endfunction()
Loading