aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qtcreatorcdbext/CMakeLists.txt
blob: 2bb2e21bf2478474373afd8c9e52f39ccc4f211d (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
cmake_minimum_required(VERSION 3.20)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake")

project(qtcreatorcdbext)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if (NOT DEFINED QTCREATORCDBEXT_INSTALL_LLVM)
  set(QTCREATORCDBEXT_INSTALL_LLVM YES) # default
endif()

set(ArchSuffix "32")
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  set(ArchSuffix "64")
endif()

if(MSVC_CXX_ARCHITECTURE_ID MATCHES "^ARM")
  set(ArchSuffix "arm${ArchSuffix}")
endif()

if (NOT QT_CREATOR_API_DEFINED)
  # standalone build
  set(QTC_SBOM_READY ON)
  include(QtCreatorIDEBranding)
  include(QtCreatorAPI)
  include(QtCreatorSbom)
  qtc_handle_compiler_cache_support()

  # Need to look for Qt6, so that we can use the SBOM feature, even though the
  # project doesn't link to Qt.
  # Explicitly disable the version checks, because when configuring for an architecture that
  # is not compatible with the found Qt, we'd fail to find the package. We don't care about
  # binary compatibility here, we only want the SBOM feature.
  if(QT_GENERATE_SBOM)
    set(QT_NO_PACKAGE_VERSION_CHECK ON)
    set(QT_NO_PACKAGE_VERSION_INCOMPATIBLE_WARNING ON)
    find_package(Qt6 REQUIRED)
    unset(QT_NO_PACKAGE_VERSION_CHECK)
    unset(QT_NO_PACKAGE_VERSION_INCOMPATIBLE_WARNING)
  endif()

  set(QT_CREATOR_CDB_EXT_STANDALONE_BUILD TRUE)

  # Compile for x86, x64 and arm64
  if (NOT ${PROJECT_NAME}-MultiBuild AND NOT MINGW)
    include(ExternalProject)

    set(generator "Visual Studio 16 2019")
    if(CMAKE_CXX_COMPILER MATCHES "Microsoft Visual Studio/2022/")
      set(generator "Visual Studio 17 2022")
    endif()

    string(REPLACE ";" "|" CMAKE_PREFIX_PATH_ALT_SEP "${CMAKE_PREFIX_PATH}")

    set(extra_cmake_args "")
    set(extra_install_commands "")
    if(QT_GENERATE_SBOM)
      list(APPEND extra_cmake_args -DQT_GENERATE_SBOM=${QT_GENERATE_SBOM})
      list(APPEND extra_install_commands
        && ${CMAKE_COMMAND} --install . --config ${CMAKE_BUILD_TYPE}
                            --prefix "${CMAKE_BINARY_DIR}" --component sbom
      )
    endif()

    macro (setup_library arch install_llvm)
      ExternalProject_Add(${arch}-bld
        SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
        CMAKE_GENERATOR "${generator}"
        CMAKE_GENERATOR_PLATFORM "${arch}"
        USES_TERMINAL_INSTALL TRUE
        LIST_SEPARATOR |
        CMAKE_ARGS
          -D${PROJECT_NAME}-MultiBuild=ON
          -DPythonTargetArchDll=${PythonTarget${arch}Dll}
          -DPython3_ROOT_DIR=${Python3_ROOT_DIR}
          -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH_ALT_SEP}
          -DQTCREATORCDBEXT_INSTALL_LLVM=${install_llvm}
          -DCMAKE_CONFIGURATION_TYPES=${CMAKE_BUILD_TYPE}
          ${extra_cmake_args}
        BUILD_COMMAND
          ${CMAKE_COMMAND} --build . --config ${CMAKE_BUILD_TYPE}
        INSTALL_COMMAND
          ${CMAKE_COMMAND} --install . --config ${CMAKE_BUILD_TYPE}
                           --prefix "${CMAKE_BINARY_DIR}" --component qtcreatorcdbext
          ${extra_install_commands}
      )
    endmacro()

    if (NOT QTCREATORCDBEXT_BUILD_ARCHS)
      set(QTCREATORCDBEXT_BUILD_ARCHS arm64 win32 x64)
    endif()
    set(install_llvm YES)
    foreach(arch IN LISTS QTCREATORCDBEXT_BUILD_ARCHS)
      setup_library(${arch} ${install_llvm})
      set(install_llvm NO)
    endforeach()

    list(LENGTH QTCREATORCDBEXT_BUILD_ARCHS build_archs_length)
    if (build_archs_length GREATER 0)
      install(
        DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        DESTINATION .
        COMPONENT qtcreatorcdbext
      )
      install(CODE
        "if (EXISTS \"${CMAKE_BINARY_DIR}/bin\")
          message(\"Copying ${CMAKE_BINARY_DIR}/bin to ${CMAKE_INSTALL_PREFIX}\")
          file(COPY \"${CMAKE_BINARY_DIR}/bin\" DESTINATION \"${CMAKE_INSTALL_PREFIX}\")
         endif()"
        COMPONENT qtcreatorcdbext
      )
      if(QT_GENERATE_SBOM)
        qtc_sbom_get_sbom_install_path(sbom_install_path)
        # Because we are installing a dir, the destination should be the same without the dir
        # name.
        get_filename_component(sbom_install_dest "${sbom_install_path}" DIRECTORY)
        install(
          DIRECTORY "${CMAKE_BINARY_DIR}/${sbom_install_path}"
          DESTINATION "${sbom_install_dest}"
          COMPONENT qtcreatorcdbext
        )
      endif()
    endif()

    return()
  endif()
endif()

if (NOT WIN32 OR NOT MSVC OR BUILD_DESIGNSTUDIO)
  return()
endif()

if (NOT EXISTS "${CMAKE_BINARY_DIR}/lib/qtcreatorcdbext${ArchSuffix}")
  file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/lib/qtcreatorcdbext${ArchSuffix}")
endif()

if(QT_CREATOR_CDB_EXT_STANDALONE_BUILD AND QT_GENERATE_SBOM)
  qtc_setup_sbom()
  qtc_sbom_compute_cpe(project_cpe
    VENDOR "qt"
    PRODUCT "${PROJECT_NAME}"
    VERSION "${IDE_VERSION}"
  )
  set(QT_SBOM_LICENSE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../../LICENSES")
  qtc_sbom_begin_project(
    SBOM_PROJECT_NAME "${PROJECT_NAME}-${ArchSuffix}"
    QT_REPO_PROJECT_NAME "${PROJECT_NAME}-${ArchSuffix}"
    PURL_NAMESPACE "qt"
    PURL_NAME "${PROJECT_NAME}"
    CPE "${project_cpe}"
  )
endif()

add_qtc_library(qtcreatorcdbext SHARED
  COMPONENT qtcreatorcdbext
  DEPENDS dbgeng
  DESTINATION lib/qtcreatorcdbext${ArchSuffix}/
  SOURCES
    common.cpp common.h
    containers.cpp containers.h
    eventcallback.cpp eventcallback.h
    extensioncontext.cpp extensioncontext.h
    gdbmihelpers.cpp gdbmihelpers.h
    iinterfacepointer.h
    knowntype.h
    outputcallback.cpp outputcallback.h
    qtcreatorcdbext.def
    qtcreatorcdbextension.cpp
    stringutils.cpp stringutils.h
    symbolgroup.cpp symbolgroup.h
    symbolgroupnode.cpp symbolgroupnode.h
    symbolgroupvalue.cpp symbolgroupvalue.h
)

qtc_library_enabled(_library_enabled qtcreatorcdbext)
if (_library_enabled)
  # statically link MSVC runtime
  set_property(TARGET qtcreatorcdbext PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
  target_compile_options(qtcreatorcdbext PUBLIC /EHsc)

  find_package(Python3 3.8 COMPONENTS Development)

  if (NOT ${Python3_Development_FOUND})
    message(WARNING "PythonLibs (at least version 3.8) not found. qtcreatorcdbext will be built without Python support.")
    return()
  endif()

  set(PythonRegex "^(.*)/(.*)/(python([0-9]+))${CMAKE_IMPORT_LIBRARY_SUFFIX}$")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(PythonRegex "^(.*)/(.*)/(python([0-9]+)_d)${CMAKE_IMPORT_LIBRARY_SUFFIX}$")
  endif()

  foreach(lib IN LISTS Python3_LIBRARIES)
    if (lib MATCHES ${PythonRegex})
      if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        set(PythonZipFileName "python${CMAKE_MATCH_4}_d.zip")
      else()
        set(PythonZipFileName "python${CMAKE_MATCH_4}.zip")
      endif()
      set(PythonNameWithVersion "${CMAKE_MATCH_3}")

      set(PythonDll "${CMAKE_MATCH_1}/${PythonNameWithVersion}${CMAKE_SHARED_LIBRARY_SUFFIX}")
      set(PythonExe "${CMAKE_MATCH_1}/python${CMAKE_EXECUTABLE_SUFFIX}")
      set(PythonZip "${CMAKE_MATCH_1}/${PythonZipFileName}")

      break()
    endif()
  endforeach()

  if (NOT PythonDll)
    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
      message(WARNING "The Debug build of Qt Creator requires Debug Python libraries. Please check your Python installation")
    endif()
    message(WARNING "PythonDll not found. qtcreatorcdbext will be built without Python support.")
    return()
  endif()

  # Support for cross-compilation for arm64 on a x64 system
  if (MSVC_CXX_ARCHITECTURE_ID STREQUAL "ARM64" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "ARM64")
    set(arm64_on_arm64 ON)
  endif()
  if (MSVC_CXX_ARCHITECTURE_ID STREQUAL "x64" AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")
    set(x64_on_x64 ON)
  endif()

  if (NOT arm64_on_arm64 AND NOT x64_on_x64)
    find_program(dumpbin_executable dumpbin)
    find_program(lib_executable lib)

    string(TOLOWER ${MSVC_CXX_ARCHITECTURE_ID} lower_arch_name)
    if (lower_arch_name STREQUAL "arm64")
      set(python_suffix "arm64")
    elseif (lower_arch_name STREQUAL "x64")
      set(python_suffix "amd64")
    else()
      set(python_suffix "win32")
    endif()

    if (NOT dumpbin_executable OR NOT lib_executable)
      message(WARNING "Couldn't locate dumpbin.exe or lib.exe executables")
      return()
    endif()

    if (Python3_VERSION VERSION_LESS "3.11.0" AND lower_arch_name STREQUAL "arm64")
      message(WARNING "Python 3.11.0 needs to be installed. This version is the first version that has arm64 Windows support")
      return()
    endif()

    file(TO_NATIVE_PATH ${PythonDll} NativePythonDll)
    execute_process(
      COMMAND ${dumpbin_executable} /exports ${NativePythonDll}
      OUTPUT_VARIABLE dumpbin_output
      RESULT_VARIABLE dumpbin_result)

    string(REGEX REPLACE ".*[ \t]+ordinal[ \t]+hint[ \t]+RVA[ \t]+name[\r\n][\r\n]" "" dumpbin_output "${dumpbin_output}")
    string(REGEX REPLACE "[\r\n][ \t]+Summary[\r\n].*" "" dumpbin_output "${dumpbin_output}")
    string(REGEX REPLACE "([ \t]+[0-9]+)([ \t]+[a-fA-F0-9]+)([ \t]+[a-fA-F0-9]+)[ \t]+([a-zA-Z0-9_]+)( = [a-zA-Z0-9_]+[\r\n]|[\r\n])" "\\4;" filter_output "${dumpbin_output}")

    string(APPEND pythondef "LIBRARY ${PythonNameWithVersion}\nEXPORTS\n")
    foreach(var IN LISTS filter_output)
      if (var)
        string(APPEND pythondef "${var}\n")
      endif()
    endforeach()
    file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${PythonNameWithVersion}.def "${pythondef}")

    execute_process(
      COMMAND "${lib_executable}"
              /def:${CMAKE_CURRENT_BINARY_DIR}/${PythonNameWithVersion}.def
              /out:${CMAKE_CURRENT_BINARY_DIR}/${PythonNameWithVersion}.lib /machine:${lower_arch_name} /nologo)
    set(Python3_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/${PythonNameWithVersion}.lib")

    if (NOT PythonTargetArchDll AND ENV{PythonTargetArchDll})
      set(PythonTargetArchDll $ENV{PythonTargetArchDll})
    endif()

    if (NOT PythonTargetArchDll)
      set(python_embed_url "https://www.python.org/ftp/python/${Python3_VERSION}/python-${Python3_VERSION}-embed-${python_suffix}.zip")
      message(STATUS "Downloading ${python_embed_url}")

      foreach(retry RANGE 10)
        file(DOWNLOAD ${python_embed_url} ${CMAKE_CURRENT_BINARY_DIR}/python-embed.zip)
        file(SIZE ${CMAKE_CURRENT_BINARY_DIR}/python-embed.zip fileSize)
        if (fileSize GREATER 0)
          break()
        endif()
      endforeach()

      file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/python-embed)
      file(ARCHIVE_EXTRACT INPUT ${CMAKE_CURRENT_BINARY_DIR}/python-embed.zip DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/python-embed)

      set(PythonTargetArchDll ${CMAKE_CURRENT_BINARY_DIR}/python-embed/${PythonNameWithVersion}${CMAKE_SHARED_LIBRARY_SUFFIX})
    endif()

    if (NOT PythonTargetArchDll)
      message(WARNING "PythonTargetArchDll CMake parameter or ENV{PythonTargetArchDll} was not configured and the Python runtime cannot be configured")
      return()
    endif()

    set(PythonDll "${PythonTargetArchDll}")
  endif()

  extend_qtc_library(qtcreatorcdbext
    DEPENDS "${Python3_LIBRARIES}"
    INCLUDES "${Python3_INCLUDE_DIRS}"
    DEFINES WITH_PYTHON=1 PY_SSIZE_T_CLEAN
    SOURCES
      pycdbextmodule.cpp pycdbextmodule.h
      pyfield.cpp pyfield.h
      pystdoutredirect.cpp pystdoutredirect.h
      pytype.cpp pytype.h
      pyvalue.cpp pyvalue.h
  )

  if (NOT EXISTS "${PythonZip}" AND
      NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
    include(CreatePythonXY)
    create_python_xy("${PythonExe}" "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
  endif()

  if (NOT EXISTS "${PythonZip}" AND
      EXISTS "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
    set(PythonZip "${CMAKE_CURRENT_BINARY_DIR}/${PythonZipFileName}")
  endif()

  list(APPEND deployPythonFiles "${PythonDll}")
  list(APPEND deployPythonFiles "${PythonZip}")

  install(FILES ${deployPythonFiles}
          DESTINATION lib/qtcreatorcdbext${ArchSuffix}/
          COMPONENT qtcreatorcdbext)

  add_custom_target(copy_python_dll ALL VERBATIM)

  qtc_output_binary_dir(output_binary_dir)
  add_custom_command(TARGET copy_python_dll POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${deployPythonFiles} "${output_binary_dir}/lib/qtcreatorcdbext${ArchSuffix}/"
    VERBATIM
  )

  if (QTCREATORCDBEXT_INSTALL_LLVM)
    # Deploy lldb.exe and its Python dependency
    find_package(Clang QUIET)
    if (LLVM_TOOLS_BINARY_DIR AND LLVM_LIBRARY_DIRS)
      file(GLOB python_files RELATIVE ${LLVM_TOOLS_BINARY_DIR} "${LLVM_TOOLS_BINARY_DIR}/python*")
      foreach(lldb_file lldb.exe lldb-dap.exe liblldb.dll ${python_files})
        if (EXISTS ${LLVM_TOOLS_BINARY_DIR}/${lldb_file})
          install(FILES ${LLVM_TOOLS_BINARY_DIR}/${lldb_file}
                  DESTINATION bin/clang/bin
                  COMPONENT qtcreatorcdbext)
        endif()
      endforeach()

      if (EXISTS ${LLVM_LIBRARY_DIRS}/site-packages)
        install(DIRECTORY ${LLVM_LIBRARY_DIRS}/site-packages
                DESTINATION bin/clang/lib
                COMPONENT qtcreatorcdbext
                PATTERN "_lldb.cp*64.pyd" EXCLUDE)
      endif()
    endif()
  endif()

endif()

if(QT_CREATOR_CDB_EXT_STANDALONE_BUILD AND QT_GENERATE_SBOM)
  qtc_sbom_end_project()
endif()