Skip to content

Commit

Permalink
feat: Parse -S and -B in rocsetup.cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
zchrissirhcz committed Nov 24, 2024
1 parent 3f7e61c commit 092a561
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Usage:
```pwsh
cmake -P rocsetup.cmake -p vs2022 -a x64
cmake -P rocsetup.cmake -p vs2022 -a x64 -S . -B build
```

Will parse and then run:
Expand Down
24 changes: 16 additions & 8 deletions rocbuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ endfunction()
# dependencies of the executable target have been defined, otherwise some .dlls might not be copied to the target
# folder.
function(rocbuild_copy_dlls target)
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.21")
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}> $<TARGET_FILE_DIR:${target}>
COMMAND_EXPAND_LISTS
)
return()
endif()

# Sanity checks
if(CMAKE_CROSSCOMPILING OR (NOT WIN32))
return()
Expand All @@ -116,6 +108,14 @@ function(rocbuild_copy_dlls target)
message(FATAL_ERROR "rocbuild_copy_dlls() was called on a non-executable target: ${target}")
endif()

if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.21")
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}> $<TARGET_FILE_DIR:${target}>
COMMAND_EXPAND_LISTS
)
return()
endif()

# set the name of file to be written
if(DEFINED CMAKE_CONFIGURATION_TYPES)
set(COPY_SCRIPT "${CMAKE_BINARY_DIR}/rocbuild_copy_dlls_${target}_$<CONFIG>.cmake")
Expand Down Expand Up @@ -298,5 +298,13 @@ function(rocbuild_remove_unused_data_and_function TARGET)
endfunction()


function(rocbuild_print_args)
message(STATUS "ROCBUILD/I: CMake version: ${CMAKE_VERSION}")
message(STATUS "ROCBUILD/I: ROCBUILD_PLATFORM: ${ROCBUILD_PLATFORM}")
message(STATUS "ROCBUILD/I: ROCBUILD_ARCH: ${ROCBUILD_ARCH}")
endfunction()


rocbuild_print_args()
rocbuild_set_artifacts_path()
rocbuild_enable_ninja_colorful_output()
66 changes: 52 additions & 14 deletions rocsetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ function(print_args)
endfunction()


function(print_original_args)
set(all_args)
foreach(i RANGE 0 ${CMAKE_ARGC})
set(all_args "${all_args} ${CMAKE_ARGV${i}}")
endforeach()
message(STATUS "ROCSETUP/I: Raw command:${all_args}")
endfunction()


function(parse_args)
math(EXPR LAST_INDEX "${CMAKE_ARGC}-1")
set(options -p -a)
set(options -p -a -S -B)
set(current_option "")
foreach(i RANGE 1 ${LAST_INDEX})
# message("Argument ${i}: ${CMAKE_ARGV${i}}")
Expand All @@ -22,6 +31,10 @@ function(parse_args)
set(ROCBUILD_PLATFORM "${arg}" PARENT_SCOPE)
elseif(current_option STREQUAL "-a")
set(ROCBUILD_ARCH "${arg}" PARENT_SCOPE)
elseif(current_option STREQUAL "-S")
set(ROCBUILD_SOURCE_DIR "${arg}" PARENT_SCOPE)
elseif(current_option STREQUAL "-B")
set(ROCBUILD_BINARY_DIR "${arg}" PARENT_SCOPE)
endif()
set(current_option "")
endif()
Expand All @@ -46,22 +59,41 @@ function(set_generator)
set(ROCBUILD_GENERATOR_EXTRA ${ROCBUILD_GENERATOR_EXTRA} PARENT_SCOPE)
endfunction()


print_original_args()
#print_args()
parse_args()
set_generator()

message(STATUS "ROCBUILD_PLATFORM: ${ROCBUILD_PLATFORM}")
message(STATUS "ROCBUILD_ARCH: ${ROCBUILD_ARCH}")
message(STATUS "ROCBUILD_GENERATOR: ${ROCBUILD_GENERATOR}")
set(cmake_arguments)

set(cmake_arguments
-S . -B build
-G ${ROCBUILD_GENERATOR} ${ROCBUILD_GENERATOR_EXTRA}
-DROCBUILD_PLATFORM=${ROCBUILD_PLATFORM} -DROCBUILD_ARCH=${ROCBUILD_ARCH}
)
if(ROCBUILD_SOURCE_DIR)
list(APPEND cmake_arguments -S ${ROCBUILD_SOURCE_DIR})
endif()

if(ROCBUILD_BINARY_DIR)
list(APPEND cmake_arguments -B ${ROCBUILD_BINARY_DIR})
endif()

if(ROCBUILD_GENERATOR)
list(APPEND cmake_arguments -G ${ROCBUILD_GENERATOR} ${ROCBUILD_GENERATOR_EXTRA})
endif()

if(ROCBUILD_PLATFORM)
list(APPEND cmake_arguments -DROCBUILD_PLATFORM=${ROCBUILD_PLATFORM})
else()
message(FATAL_ERROR "Platform not specified")
endif()

if(ROCBUILD_ARCH)
list(APPEND cmake_arguments -DROCBUILD_ARCH=${ROCBUILD_ARCH})
else()
message(FATAL_ERROR "Architecture not specified")
endif()

string(REPLACE ";" " " cmake_arguments_str "${cmake_arguments}")
message(STATUS "CMake arguments: ${cmake_arguments_str}")
set(parsed_command "cmake ${cmake_arguments}")
string(REPLACE ";" " " parsed_command_str "${parsed_command}")
message(STATUS "ROCSETUP/I: Parsed command: ${parsed_command_str}")
execute_process(
COMMAND cmake ${cmake_arguments}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Expand All @@ -70,7 +102,13 @@ execute_process(
ERROR_VARIABLE error_output
)

message("${result}")
message("${output}")
message("${error_output}")
if(result)
message("${result}")
endif()
if(output)
message("${output}")
endif()
if(error_output)
message("${error_output}")
endif()

0 comments on commit 092a561

Please sign in to comment.