compor / llvm-ir-cmake-utils

LLVM IR CMake utils for bitcode file manipulation by opt and friends
GNU Lesser General Public License v2.1
66 stars 14 forks source link

use cmake generate expression to simplify some jobs #37

Open WuTUT opened 4 weeks ago

WuTUT commented 4 weeks ago

I think there is a simple way to implement these functions

if use cmake generate expressions , each function will be only one line.

  set(CMD_ARGS "-emit-llvm" ${IN_STANDARD_FLAGS} ${IN_LANG_FLAGS})
  add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
    COMMAND ${LLVMIR_COMPILER} ${CMD_ARGS}
    "$<JOIN:$<TARGET_PROPERTY:${DEPENDS_TRGT},COMPILE_OPTIONS>,;>"
    "$<JOIN:$<TARGET_PROPERTY:${DEPENDS_TRGT},COMPILE_FLAGS>,;>"
    "-D$<JOIN:$<TARGET_PROPERTY:${DEPENDS_TRGT},COMPILE_DEFINITIONS>,;-D>"
    "-I$<JOIN:$<TARGET_PROPERTY:${DEPENDS_TRGT},INCLUDE_DIRECTORIES>,;-I>"
    -c ${ABS_IN_FILE} -o ${FULL_OUT_LLVMIR_FILE}
    DEPENDS ${ABS_IN_FILE}
    IMPLICIT_DEPENDS ${LINKER_LANGUAGE} ${ABS_IN_FILE}
    COMMENT "Generating LLVM bitcode ${OUT_LLVMIR_FILE}"
    COMMAND_EXPAND_LISTS
    VERBATIM)

In addition, there is an important benefit: there is no need to recursively fetch the include directories introduced by target_link_library. For instance, if a library is linked publicly, its interface headers will also be visible. I test in my complicated project and it finds all include paths correctly.

The drawback may be the dependency on a higher version of CMake. However, I haven't tested whether this code works on cmake-version 3.0.

compor commented 5 days ago

Hi there, the support for generator expressions has evolved since before v3.0. As far as I know, things like TARGET_PROPERTY do not exist in version 3.0 and I'd like to keep this as widely available as possible.

Speed was never a concern.