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

Add support for building your own *.csproj files #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ Parts of this project based on code by Illumina.

For more info:

https://github.com/Illumina/interop
https://github.com/Illumina/interop

## `.csproj` support
It is possible to include eg. a C# project `.csproj` file with the `add_dotnet_project` function.
This `.csproj` file must include a line

```xml
<Import Project="obj/CMake.g.props"/>
```

as this is where CMake generates references to other ROS 2 packages in, as well as the output path and the assembly name.
73 changes: 73 additions & 0 deletions cmake/Modules/FindDotNETExtra.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,79 @@ function(add_dotnet_test _TARGET_NAME)

endfunction()

function(add_dotnet_library_project _TARGET_NAME)
cmake_parse_arguments(_add_dotnet_library_project
""
""
"PROJ;INCLUDE_DLLS"
${ARGN}
)

csharp_add_existing_project(${_TARGET_NAME}
PROJ
${_add_dotnet_library_project_PROJ}
${_add_dotnet_library_project_UNPARSED_ARGUMENTS}
INCLUDE_DLLS
${_add_dotnet_library_project_INCLUDE_DLLS}
)
endfunction()

function(add_dotnet_executable_project _TARGET_NAME)
cmake_parse_arguments(_add_dotnet_executable_project
""
""
"PROJ;INCLUDE_DLLS"
${ARGN}
)

csharp_add_existing_project(${_TARGET_NAME}
EXECUTABLE
PROJ
${_add_dotnet_executable_project_PROJ}
${_add_dotnet_executable_project_UNPARSED_ARGUMENTS}
INCLUDE_DLLS
${_add_dotnet_executable_project_INCLUDE_DLLS}
)
endfunction()

function(add_dotnet_test_project _TARGET_NAME)
# TODO: (sh) It seems the test project gets build twice with different output directories
# e.g.: the same output files are contained in "build/<package>/<target>/net6.0/" and "build/<package>/<target>/net6.0/linux-x64"
# But this seems to be the case with other projects as well (package rcldotnet and targets rcldotnet_assemblies and test_messages).
# So maybe this is how it should be, but why?

cmake_parse_arguments(_add_dotnet_test_project
""
""
"PROJ;INCLUDE_DLLS"
${ARGN}
)

csharp_add_existing_project(${_TARGET_NAME}
EXECUTABLE
PROJ
${_add_dotnet_test_project_PROJ}
${_add_dotnet_test_project_UNPARSED_ARGUMENTS}
INCLUDE_DLLS
${_add_dotnet_test_project_INCLUDE_DLLS}
)

if(CSBUILD_PROJECT_DIR)
set(CURRENT_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${CSBUILD_PROJECT_DIR}")
else()
set(CURRENT_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
endif()

get_filename_component(_add_dotnet_test_project_PROJ_ABSOLUTE ${_add_dotnet_test_project_PROJ} ABSOLUTE)

ament_add_test(
${_TARGET_NAME}
GENERATE_RESULT_FOR_RETURN_CODE_ZERO
WORKING_DIRECTORY ${CURRENT_TARGET_BINARY_DIR}/${_TARGET_NAME}
COMMAND dotnet test ${_add_dotnet_test_project_PROJ_ABSOLUTE}
)
endfunction()

function(install_dotnet _TARGET_NAME)
get_target_property(_target_executable ${_TARGET_NAME} EXECUTABLE)
get_target_property(_target_path ${_TARGET_NAME} OUTPUT_PATH)
Expand Down
8 changes: 8 additions & 0 deletions cmake/Modules/dotnet/CMake.g.props.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<ItemGroup>
@CSHARP_BUILDER_INCLUDE_DLLS_STR@ </ItemGroup>
<PropertyGroup>
<OutputPath>@CSHARP_BUILDER_OUTPUT_PATH_NATIVE@</OutputPath>
<AssemblyName>@_TARGET_NAME@</AssemblyName>
</PropertyGroup>
</Project>
79 changes: 79 additions & 0 deletions cmake/Modules/dotnet/UseCSharpProjectBuilder.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,82 @@ function(csharp_add_project name)
${DOTNET_CORE_FOUND}
)
endfunction()

function(csharp_add_existing_project name)
if(CSBUILD_PROJECT_DIR)
set(CURRENT_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/${CSBUILD_PROJECT_DIR}")
else()
set(CURRENT_TARGET_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
endif()
set(CSBUILD_PROJECT_DIR "")
file(MAKE_DIRECTORY ${CURRENT_TARGET_BINARY_DIR}/${name})
cmake_parse_arguments(_csharp_add_existing_project
"EXECUTABLE"
""
"PROJ;INCLUDE_DLLS"
${ARGN}
)

foreach(it ${_csharp_add_existing_project_INCLUDE_DLLS})
file(TO_NATIVE_PATH ${it} nit)
list(APPEND refs " <Reference Include=\"${nit}\"/>\n")
endforeach()

list(LENGTH refs REFERENCE_COUNT)

if(REFERENCE_COUNT GREATER 0)
set(refs_concat "${refs}")
else()
set(refs_concat "")
endif()

get_filename_component(_csharp_add_existing_project_PROJ_PATH ${_csharp_add_existing_project_PROJ} DIRECTORY)
get_filename_component(_csharp_add_existing_project_PROJ_PATH_ABSOLUTE ${_csharp_add_existing_project_PROJ_PATH} ABSOLUTE)

get_filename_component(_csharp_add_existing_project_PROJ_ABSOLUTE ${_csharp_add_existing_project_PROJ} ABSOLUTE)

set(_csharp_add_existing_project_PROPS_PATH ${_csharp_add_existing_project_PROJ_PATH_ABSOLUTE}/obj/CMake.g.props)

set(CSHARP_BUILDER_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${name}/${CMAKE_BUILD_TYPE})
file(TO_NATIVE_PATH ${CSHARP_BUILDER_OUTPUT_PATH} CSHARP_BUILDER_OUTPUT_PATH_NATIVE)

# TODO: add to add_custom_target to avoid writing every time
string (REPLACE ";" "" CSHARP_BUILDER_INCLUDE_DLLS_STR "${refs_concat}")
set(CSHARP_BUILDER_INCLUDE_DLLS ${refs_concat})
configure_file(${dotnet_cmake_module_DIR}/Modules/dotnet/CMake.g.props.in ${_csharp_add_existing_project_PROPS_PATH} @ONLY)

if(${_csharp_add_existing_project_EXECUTABLE} AND NOT DOTNET_CORE_FOUND)
set(ext "exe")
else()
set(ext "dll")
endif()

add_custom_target(
${name} ALL

COMMAND ${RESTORE_CMD}

COMMAND ${CSBUILD_EXECUTABLE} ${CSBUILD_RESTORE_FLAGS} ${_csharp_add_existing_project_PROJ_ABSOLUTE}
COMMAND ${CSBUILD_EXECUTABLE} ${CSBUILD_BUILD_FLAGS} ${_csharp_add_existing_project_PROJ_ABSOLUTE}
WORKING_DIRECTORY ${CURRENT_TARGET_BINARY_DIR}/${name}
COMMENT "${RESTORE_CMD};${CSBUILD_EXECUTABLE} ${CSBUILD_RESTORE_FLAGS} ${_csharp_add_existing_project_PROJ_ABSOLUTE}; ${CSBUILD_EXECUTABLE} ${CSBUILD_BUILD_FLAGS} ${_csharp_add_existing_project_PROJ_ABSOLUTE} -> ${CURRENT_TARGET_BINARY_DIR}/${name}"

# TODO: How to deal with changes to *.cs files (Implicit include in *.csproj)
# DEPENDS ${sources_dep}
DEPENDS ${_csharp_add_existing_project_PROJ_ABSOLUTE}
)

set(DOTNET_OUTPUT_PATH ${CSHARP_BUILDER_OUTPUT_PATH}/${CSHARP_TARGET_FRAMEWORK}/${DOTNET_CORE_RUNTIME}/publish/)

set_target_properties(${name}
PROPERTIES
EXECUTABLE
${_csharp_add_existing_project_EXECUTABLE}
OUTPUT_PATH
${DOTNET_OUTPUT_PATH}
OUTPUT_NAME
${name}${CSBUILD_OUTPUT_SUFFIX}.${ext}
DOTNET_CORE
${DOTNET_CORE_FOUND}
)
endfunction()