summaryrefslogtreecommitdiffstats
path: root/cmake/scripts/windowsstore/Macros.cmake
blob: 713e8788b0ba8388555725b4f46e42ba401de1a6 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function(core_link_library lib wraplib)
  message(AUTHOR_WARNING "core_link_library is not compatible with windows.")
endfunction()

function(find_soname lib)
  # Windows uses hardcoded dlls in xbmc/DllPaths_win32.h.
  # Therefore the output of this function is unused.
endfunction()

# Add precompiled header to target
# Arguments:
#   target existing target that will be set up to compile with a precompiled header
#   pch_header the precompiled header file
#   pch_source the precompiled header source file
# Optional Arguments:
#   PCH_TARGET build precompiled header as separate target with the given name
#              so that the same precompiled header can be used for multiple libraries
#   EXCLUDE_SOURCES if not all target sources shall use the precompiled header,
#                   the relevant files can be listed here
# On return:
#   Compiles the pch_source into a precompiled header and adds the header to
#   the given target
function(add_precompiled_header target pch_header pch_source)
  cmake_parse_arguments(PCH "" "PCH_TARGET" "EXCLUDE_SOURCES" ${ARGN})

  if(PCH_PCH_TARGET)
    set(pch_binary ${PRECOMPILEDHEADER_DIR}/${PCH_PCH_TARGET}.pch)
  else()
    set(pch_binary ${PRECOMPILEDHEADER_DIR}/${target}.pch)
  endif()

  # Set compile options and dependency for sources
  get_target_property(sources ${target} SOURCES)
  list(REMOVE_ITEM sources ${pch_source})
  foreach(exclude_source IN LISTS PCH_EXCLUDE_SOURCES)
    list(REMOVE_ITEM sources ${exclude_source})
  endforeach()
  set_source_files_properties(${sources}
                              PROPERTIES COMPILE_FLAGS "/Yu\"${pch_header}\" /Fp\"${pch_binary}\" /FI\"${pch_header}\""
                              OBJECT_DEPENDS "${pch_binary}")

  # Set compile options for precompiled header
  if(NOT PCH_PCH_TARGET OR NOT TARGET ${PCH_PCH_TARGET}_pch)
    set_source_files_properties(${pch_source}
                                PROPERTIES COMPILE_FLAGS "/Yc\"${pch_header}\" /Fp\"${pch_binary}\""
                                OBJECT_OUTPUTS "${pch_binary}")
  endif()

  # Compile precompiled header
  if(PCH_PCH_TARGET)
    # As own target for usage in multiple libraries
    if(NOT TARGET ${PCH_PCH_TARGET}_pch)
      add_library(${PCH_PCH_TARGET}_pch STATIC ${pch_source})
      set_target_properties(${PCH_PCH_TARGET}_pch PROPERTIES COMPILE_PDB_NAME vc140
                                                             COMPILE_PDB_OUTPUT_DIRECTORY ${PRECOMPILEDHEADER_DIR}
                                                             FOLDER "Build Utilities")
    endif()
    # From VS2012 onwards, precompiled headers have to be linked against (LNK2011).
    target_link_libraries(${target} PUBLIC ${PCH_PCH_TARGET}_pch)
    set_target_properties(${target} PROPERTIES COMPILE_PDB_NAME vc140
                                               COMPILE_PDB_OUTPUT_DIRECTORY ${PRECOMPILEDHEADER_DIR})
  else()
    # As part of the target
    target_sources(${target} PRIVATE ${pch_source})
  endif()
endfunction()

macro(winstore_set_assets target)
  file(GLOB ASSET_FILES "${CMAKE_SOURCE_DIR}/tools/windows/packaging/uwp/media/*.png")
  set_property(SOURCE ${ASSET_FILES} PROPERTY VS_DEPLOYMENT_CONTENT 1)
  set_property(SOURCE ${ASSET_FILES} PROPERTY VS_DEPLOYMENT_LOCATION "media")
  source_group("media" FILES ${ASSET_FILES})
  set(RESOURCES ${RESOURCES} ${ASSET_FILES}
                            "${CMAKE_SOURCE_DIR}/tools/windows/packaging/uwp/kodi_temp_key.pfx")

  set(LICENSE_FILES
    ${CMAKE_SOURCE_DIR}/LICENSE.md
    ${CMAKE_SOURCE_DIR}/privacy-policy.txt)
  if(EXISTS "${CMAKE_SOURCE_DIR}/known_issues.txt")
    list(APPEND LICENSE_FILES ${CMAKE_SOURCE_DIR}/known_issues.txt)
  endif()
  set_property(SOURCE ${LICENSE_FILES} PROPERTY VS_DEPLOYMENT_CONTENT 1)
  list(APPEND RESOURCES ${LICENSE_FILES})
endmacro()

macro(winstore_generate_manifest target)
  configure_file(
    ${CMAKE_SOURCE_DIR}/tools/windows/packaging/uwp/${APP_MANIFEST_NAME}.in
    ${CMAKE_CURRENT_BINARY_DIR}/${APP_MANIFEST_NAME}
    @ONLY)
  set(RESOURCES ${RESOURCES} ${CMAKE_CURRENT_BINARY_DIR}/${APP_MANIFEST_NAME})
endmacro()

macro(add_deployment_content_group path link match exclude)
  set(_link "")
  set(_exclude "")
  file(TO_NATIVE_PATH ${path} _path)
  file(TO_NATIVE_PATH ${match} _match)
  if (NOT "${link}" STREQUAL "")
    file(TO_NATIVE_PATH ${link} _link)
    set(_link "${_link}\\")
  endif()
  if(NOT "${exclude}" STREQUAL "")
    string(REPLACE "/" "\\" _exclude ${exclude})
  endif()
  string(CONCAT UWP_DEPLOYMENT_CONTENT_STR "${UWP_DEPLOYMENT_CONTENT_STR}"
    "    <EmbedResources Include=\"${_path}\\${_match}\" Exclude=\"${_exclude}\">\n"
    "      <Link>${_link}%(RecursiveDir)%(FileName)%(Extension)</Link>\n"
    "      <DeploymentContent>true</DeploymentContent>\n"
    "    </EmbedResources>\n")
endmacro()

macro(winstore_append_props target)
  # exclude debug dlls from packaging
  set(DEBUG_DLLS zlibd.dll)
  foreach(_dll ${DEBUG_DLLS})
    if (DEBUG_DLLS_EXCLUDE)
      list(APPEND DEBUG_DLLS_EXCLUDE "\;$(BuildRootPath)/dlls/${_dll}")
    else()
      list(APPEND DEBUG_DLLS_EXCLUDE "$(BuildRootPath)/dlls/${_dll}")
    endif()
    string(CONCAT DEBUG_DLLS_LINKAGE_PROPS "${DEBUG_DLLS_LINKAGE_PROPS}"
    "  <ItemGroup Label=\"Binaries\">\n"
    "    <None Include=\"$(BinPath)\\${_dll}\" Condition=\"'$(Configuration)'=='Debug'\">\n"
    "      <DeploymentContent>true</DeploymentContent>\n"
    "    </None>\n"
    "  </ItemGroup>\n")
  endforeach(_dll DEBUG_DLLS)

  add_deployment_content_group($(BuildRootPath)/dlls "" *.dll "${DEBUG_DLLS_EXCLUDE}")
  add_deployment_content_group($(BuildRootPath)/system system **/* "$(BuildRootPath)/**/shaders/**")
  add_deployment_content_group($(BuildRootPath)/system/shaders system/shaders **/*.fx "")
  add_deployment_content_group($(BuildRootPath)/media media **/* "")
  add_deployment_content_group($(BuildRootPath)/userdata userdata **/* "")
  add_deployment_content_group($(BuildRootPath)/addons addons **/* "")
  add_deployment_content_group($(BinaryAddonsPath) addons **/* "")

  foreach(xbt_file ${XBT_FILES})
    file(RELATIVE_PATH relative ${CMAKE_CURRENT_BINARY_DIR} ${xbt_file})
    file(TO_NATIVE_PATH ${relative} relative)
    string(CONCAT XBT_FILE_PROPS "${XBT_FILE_PROPS}"
    "  <ItemGroup Label=\"SkinsMedia\">\n"
    "    <None Include=\"$(BuildRootPath)\\${relative}\">\n"
    "      <Link>${relative}</Link>\n"
    "      <DeploymentContent>true</DeploymentContent>\n"
    "    </None>\n"
    "  </ItemGroup>\n")
  endforeach()

  set(VCPROJECT_PROPS_FILE "${CMAKE_CURRENT_BINARY_DIR}/${target}.props")
  file(TO_NATIVE_PATH ${DEPENDS_PATH} DEPENDENCIES_DIR_NATIVE)
  file(TO_NATIVE_PATH ${CMAKE_CURRENT_BINARY_DIR} CMAKE_CURRENT_BINARY_DIR_NATIVE)
  file(TO_NATIVE_PATH ${CMAKE_SOURCE_DIR}/project/Win32BuildSetup/BUILD_WIN32/addons BINARY_ADDONS_DIR_NATIVE)

  file(WRITE ${VCPROJECT_PROPS_FILE}
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
    "<Project ToolsVersion=\"4.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n"
    "  <ImportGroup Label=\"PropertySheets\" />\n"
    "  <PropertyGroup Label=\"APP_DLLS\">\n"
    "    <BinPath>${DEPENDENCIES_DIR_NATIVE}\\bin</BinPath>\n"
    "    <BuildRootPath>${CMAKE_CURRENT_BINARY_DIR_NATIVE}</BuildRootPath>\n"
    "    <BinaryAddonsPath>${BINARY_ADDONS_DIR_NATIVE}</BinaryAddonsPath>\n"
    "  </PropertyGroup>\n"
    "${DEBUG_DLLS_LINKAGE_PROPS}"
    "${XBT_FILE_PROPS}"
    "  <ItemGroup>\n"
    "${UWP_DEPLOYMENT_CONTENT_STR}"
    "  </ItemGroup>\n"
    "  <Target Name=\"_CollectCustomResources\" Inputs=\"@(EmbedResources)\" Outputs=\"@(EmbedResources->'$(OutputPath)\\PackageLayout\\%(Link)')\" BeforeTargets=\"AssignTargetPaths\">\n"
    "    <Message Text=\"Collecting package resources...\"/>\n"
    "    <ItemGroup>\n"
    "      <None Include=\"@(EmbedResources)\" />\n"
    "    </ItemGroup>\n"
    "  </Target>\n"
    "</Project>")
endmacro()

macro(winstore_add_target_properties target)
  winstore_set_assets(${target})
  winstore_generate_manifest(${target})
  winstore_append_props(${target})
endmacro()