CMake: Simplify qt_make_output_file

Simplify qt_make_output_file and add a simple test for it.

Change-Id: I87694291cd877545ade5d9c42d1424d7b3b7b567
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
This commit is contained in:
Tobias Hunger 2018-11-08 22:29:27 +01:00
parent fac800ad1c
commit bdb5e2a48e
3 changed files with 44 additions and 24 deletions

View File

@ -239,7 +239,7 @@ function(qt_internal_wrap_cpp result)
set(file_extension "moc")
endif()
qt_make_output_file("${it}" "${file_prefix}" "${file_extension}" outfile)
qt_make_output_file("${it}" "${file_prefix}" ".${file_extension}" "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" outfile)
qt_create_moc_command("${it}" "${outfile}" "${moc_flags}" "${moc_options}" "${moc_target}" "${moc_depends}")
set_source_files_properties(${outfile} PROPERTIES HEADER_FILE_ONLY ${_arg_HEADER_FILE_ONLY})
list(APPEND wrapped_files "${outfile}")
@ -339,7 +339,7 @@ function(qt_extract_qrc_dependencies resourceFile _out_depends _rc_depends)
# Since this cmake function is doing the dependency scanning for these files,
# let's make a configured file and add it as a dependency so cmake is run
# again when dependencies need to be recomputed.
qt_make_output_file("${infile}" "" "qrc.depends" out_depends)
qt_make_output_file("${infile}" "" ".qrc.depends" "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}" out_depends)
configure_file("${infile}" "${out_depends}" COPYONLY)
else()
# The .qrc file does not exist (yet). Let's add a dependency and hope
@ -958,32 +958,27 @@ endfunction()
# From Qt5CoreMacros
# Function used to create the names of output files preserving relative dirs
function(qt_make_output_file infile prefix ext result)
string(LENGTH ${CMAKE_CURRENT_BINARY_DIR} _binlength)
string(LENGTH ${infile} _infileLength)
set(_checkinfile ${CMAKE_CURRENT_SOURCE_DIR})
if(_infileLength GREATER _binlength)
string(SUBSTRING "${infile}" 0 ${_binlength} _checkinfile)
if(_checkinfile STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
file(RELATIVE_PATH rel ${CMAKE_CURRENT_BINARY_DIR} ${infile})
else()
file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
function(qt_make_output_file infile prefix suffix source_dir binary_dir result)
get_filename_component(outfilename "${infile}" NAME_WE)
set(base_dir "${source_dir}")
string(FIND "${infile}" "${binary_dir}/" in_binary)
if (in_binary EQUAL 0)
set(base_dir "${binary_dir}")
endif()
else()
file(RELATIVE_PATH rel ${CMAKE_CURRENT_SOURCE_DIR} ${infile})
endif()
if(WIN32 AND rel MATCHES "^([a-zA-Z]):(.*)$") # absolute path
set(rel "${CMAKE_MATCH_1}_${CMAKE_MATCH_2}")
endif()
set(_outfile "${CMAKE_CURRENT_BINARY_DIR}/${rel}")
string(REPLACE ".." "__" _outfile "${_outfile}")
get_filename_component(outpath "${_outfile}" PATH)
get_filename_component(_outfile "${_outfile}" NAME_WE)
get_filename_component(abs_infile "${infile}" ABSOLUTE BASE_DIR "${base_dir}")
file(RELATIVE_PATH rel_infile "${base_dir}" "${abs_infile}")
string(REPLACE "../" "__/" mapped_infile "${rel_infile}")
get_filename_component(abs_mapped_infile "${mapped_infile}" ABSOLUTE BASE_DIR "${binary_dir}")
get_filename_component(outpath "${abs_mapped_infile}" PATH)
file(MAKE_DIRECTORY "${outpath}")
set(full_outfile "${outpath}/${prefix}${_outfile}.${ext}")
set("${result}" "${full_outfile}" PARENT_SCOPE)
set("${result}" "${outpath}/${prefix}${outfilename}${suffix}" PARENT_SCOPE)
endfunction()
macro(qt_get_moc_flags _moc_flags)
set(${_moc_flags})
get_directory_property(_inc_DIRS INCLUDE_DIRECTORIES)

View File

@ -23,3 +23,4 @@ macro(add_test_macro NAME)
endmacro()
add_test_macro(features features)
add_test_macro(qt_make_output_file qt_make_output_file)

View File

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.12.0)
project(QtMakeOutputFileTest
VERSION 1.0.0
DESCRIPTION "qt_make_output_file test"
HOMEPAGE_URL "https://qt.io/"
LANGUAGES CXX C
)
## Add some paths to check for cmake modules:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../;${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/extra-cmake-modules/find-modules;${CMAKE_CURRENT_SOURCE_DIR}/../../3rdparty/kwin")
include(QtBuild)
qt_make_output_file("foo.cpp" "" ".mapped" "/tmp/foo" "/tmp/bar" outfile)
assert(outfile STREQUAL "/tmp/bar/foo.mapped")
qt_make_output_file("../foo.cpp" "prefix_" ".cpp" "/tmp/foo" "/tmp/bar" outfile)
assert(outfile STREQUAL "/tmp/bar/__/prefix_foo.cpp")
qt_make_output_file("/tmp/bar/foo.cpp" "prefix_" ".cpp" "/tmp/foo" "/tmp/bar" outfile)
assert(outfile STREQUAL "/tmp/bar/prefix_foo.cpp")
add_executable(qt_make_output_file ../main.cpp)