cmake配置csharp dll项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
cmake_minimum_required(VERSION 3.14)
# 名称
set(MY_PROJECT_NAME CSharpLib)
# 项目类型
project(${MY_PROJECT_NAME} LANGUAGES CSharp)
# show
message ("cmake:${PROJECT_NAME}")

# debug版本加-d后缀
# set(CMAKE_DEBUG_POSTFIX -d)

# 库目录
set(MY_SOURCE_LIB_DIR "${PROJECT_SOURCE_DIR}/../lib/")

# default Release
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE "Release")
message (
STATUS "${PROJECT_NAME} no CMAKE_BUILD_TYPE selected, defaulting to ${CMAKE_BUILD_TYPE} "
)
endif()

# AnyCPU
SET(CMAKE_CSharp_FLAGS "/platform:AnyCPU")

# .net版本
SET(CMAKE_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1")
# cs代码
add_library(${PROJECT_NAME} SHARED
Properties/AssemblyInfo.cs
LockscreenWin10.cs
)
# 系统引用
LIST(APPEND VS_DOTNET_REFERENCES "Microsoft.CSharp")
LIST(APPEND VS_DOTNET_REFERENCES "System")
LIST(APPEND VS_DOTNET_REFERENCES "System.Core")
LIST(APPEND VS_DOTNET_REFERENCES "System.Data")
LIST(APPEND VS_DOTNET_REFERENCES "System.Data.DataSetExtensions")
LIST(APPEND VS_DOTNET_REFERENCES "System.Net.Http")
LIST(APPEND VS_DOTNET_REFERENCES "System.Xml")
LIST(APPEND VS_DOTNET_REFERENCES "System.Xml.Linq")
LIST(APPEND VS_DOTNET_REFERENCES "System.Threading.Tasks")
LIST(APPEND VS_DOTNET_REFERENCES "System.IO")
#
set_target_properties(${PROJECT_NAME} PROPERTIES
VS_DOTNET_REFERENCES "${VS_DOTNET_REFERENCES}"
VS_DOTNET_REFERENCE_WindowsRuntime "System.Runtime.WindowsRuntime.dll" # 引用dll
VS_DOTNET_REFERENCE_Windows "Windows.winmd"
)
# 生成后事件
if (WIN32)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"$<$<CONFIG:Debug>:Debug/${PROJECT_NAME}.dll>"
"$<$<CONFIG:Release>:Release/${PROJECT_NAME}.dll>"
"../CSharpMiddle/")
endif() # (WIN32)