CMake是否有Autoconf中--program-prefix等安装时程序名转换机制?
Great question! Unlike Autoconf which offers built-in configure flags like --program-prefix, --program-suffix, and --program-transform-name, CMake doesn't include direct equivalents out of the box. However, you can easily replicate this behavior with custom CMake logic tailored to your project.
Implementing Program Prefix/Suffix
You can add support for prefixes and suffixes by defining custom CMake variables, which users can set when running cmake (e.g., -DPROGRAM_PREFIX=my- or -DPROGRAM_SUFFIX=-debug). Here are two common approaches:
1. Modify the Target's Output Name
Set the OUTPUT_NAME property on your executable target to include the prefix/suffix:
# Define default empty values if not provided by the user if(NOT PROGRAM_PREFIX) set(PROGRAM_PREFIX "") endif() if(NOT PROGRAM_SUFFIX) set(PROGRAM_SUFFIX "") endif() add_executable(mytool src/main.c) set_target_properties(mytool PROPERTIES OUTPUT_NAME "${PROGRAM_PREFIX}mytool${PROGRAM_SUFFIX}" ) install(TARGETS mytool DESTINATION bin)
2. Rename During Installation
Alternatively, you can rename the executable directly in the install command:
add_executable(mytool src/main.c) install(TARGETS mytool DESTINATION bin RENAME "${PROGRAM_PREFIX}mytool${PROGRAM_SUFFIX}" )
Implementing Program Transform Name (Sed-Style Replacement)
For sed-like program name transformations, you can use CMake's execute_process to run sed with the provided script, or use CMake's built-in string(REGEX REPLACE) for simpler patterns.
Using sed via execute_process
This approach mirrors Autoconf's behavior closely by accepting a full sed script:
set(original_name "mytool") add_executable(${original_name} src/main.c) if(PROGRAM_TRANSFORM_NAME) # Run sed to transform the executable name execute_process( COMMAND echo "${original_name}" COMMAND sed "${PROGRAM_TRANSFORM_NAME}" OUTPUT_VARIABLE transformed_name OUTPUT_STRIP_TRAILING_WHITESPACE ) # Update the target's output name set_target_properties(${original_name} PROPERTIES OUTPUT_NAME "${transformed_name}" ) endif() install(TARGETS ${original_name} DESTINATION bin)
Users can then pass a sed script like this:
cmake .. -DPROGRAM_TRANSFORM_NAME="s/mytool/my-utility/"
Using CMake's Built-in Regex Replacement
For simpler regex patterns, you can skip calling sed and use string(REGEX REPLACE) directly:
if(PROGRAM_TRANSFORM_PATTERN AND PROGRAM_TRANSFORM_REPLACEMENT) string(REGEX REPLACE "${PROGRAM_TRANSFORM_PATTERN}" "${PROGRAM_TRANSFORM_REPLACEMENT}" transformed_name "${original_name}") set_target_properties(${original_name} PROPERTIES OUTPUT_NAME "${transformed_name}") endif()
Users would invoke this with:
cmake .. -DPROGRAM_TRANSFORM_PATTERN="tool" -DPROGRAM_TRANSFORM_REPLACEMENT="utility"
Key Takeaway
While CMake doesn't have these flags built-in, the flexibility of its scripting language lets you implement (and even extend) Autoconf's program name transformation behavior with minimal effort. You can adjust these snippets to fit your project's specific needs, like adding validation for the input variables or supporting multiple executables.
内容的提问来源于stack exchange,提问作者Peeter Joot

