Skip to content

Commit

Permalink
add property for naming single file output targets
Browse files Browse the repository at this point in the history
  • Loading branch information
compor committed Nov 12, 2018
1 parent c0bf9a2 commit 152cd9c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 35 deletions.
80 changes: 45 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ A collection of helper `cmake` functions/macros that eases the generation of `LL
## Requirements

- [`cmake`][1] 3.0.0 or later
- [`LLVM`][2] tools:
- Currently used:
- [`LLVM`][2] tools:
- Currently used:
- `clang/clang++`
- `opt`
- `llvm-dis` / `llvm-as`
- `llvm-link`
- Tested with:
- Tested with:
- 3.7 and later

## Installation
Expand All @@ -32,38 +32,48 @@ easily supported (depending how nice it plays with `cmake` too).

The `cmake` calls currently provided are:

- `llvmir_attach_bc_target()`
Attaches to an existing target that can be compiled down to `LLVM IR` and does just that, using all the related flags
and options from the main target. The existing supported targets make use of `clang/clang++`, so currently this means
that the `C/C++` language is supported. It uses `add_custom_library()` `cmake` command under the hood. This creates a
target of type `LLVMIR`.

- `llvmir_attach_opt_pass_target()`
Attaches to a target of type `LLVMIR` and applies various `opt` passes to its bitcode files, specified as arguments.
It uses `add_custom_library()` `cmake` command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_disassemble_target()`
Attaches to a target of type `LLVMIR` and uses `llvm-dis` to disassemble its bitcode files. It uses
`add_custom_library()` `cmake` command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_assemble_target()`
Attaches to a target of type `LLVMIR` and uses `llvm-as` to assemble its bitcode files. It uses `add_custom_library()`
`cmake` command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_link_target()`
Attaches to a target of type `LLVMIR` and uses `llvm-link` to link its bitcode files to a single bitcode file. The
output bitcode file is names after the target name. It uses `add_custom_library()` `cmake` command under the hood.
This creates a target of type `LLVMIR`.

- `llvmir_attach_library()`
Attaches to a target of type `LLVMIR` and uses the appropriate compiler to compile its bitcode files to a native
library. The output library name uses the target name according to platform rules. It uses `add_library()` `cmake`
command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_executable()`
Attaches to a target of type `LLVMIR` and uses the appropriate compiler to compile its bitcode files to a native
executable. The output library name uses the target name according to platform rules. It uses `add_executable()`
`cmake` command under the hood. This creates a target of type `LLVMIR`.
- `llvmir_attach_bc_target()`
Attaches to an existing target that can be compiled down to `LLVM IR` and does just that, using all the related flags
and options from the main target. The existing supported targets make use of `clang/clang++`, so currently this means
that the `C/C++` language is supported. It uses `add_custom_library()` `cmake` command under the hood. This creates a
target of type `LLVMIR`.

- `llvmir_attach_opt_pass_target()`
Attaches to a target of type `LLVMIR` and applies various `opt` passes to its bitcode files, specified as arguments.
It uses `add_custom_library()` `cmake` command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_disassemble_target()`
Attaches to a target of type `LLVMIR` and uses `llvm-dis` to disassemble its bitcode files. It uses
`add_custom_library()` `cmake` command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_assemble_target()`
Attaches to a target of type `LLVMIR` and uses `llvm-as` to assemble its bitcode files. It uses `add_custom_library()`
`cmake` command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_link_target()`
Attaches to a target of type `LLVMIR` and uses `llvm-link` to link its bitcode files to a single bitcode file. The
output bitcode file is names after the target name. It uses `add_custom_library()` `cmake` command under the hood.
This creates a target of type `LLVMIR`.

- `llvmir_attach_library()`
Attaches to a target of type `LLVMIR` and uses the appropriate compiler to compile its bitcode files to a native
library. The output library name uses the target name according to platform rules. It uses `add_library()` `cmake`
command under the hood. This creates a target of type `LLVMIR`.

- `llvmir_attach_executable()`
Attaches to a target of type `LLVMIR` and uses the appropriate compiler to compile its bitcode files to a native
executable. The output library name uses the target name according to platform rules. It uses `add_executable()`
`cmake` command under the hood. This creates a target of type `LLVMIR`.

### Influential properties

- `LLVMIR_SHORT_NAME`
This property, if present, controls the output name for the calls that produce a single object (e.g. archive, library,
etc.):
- `llvmir_attach_link_target()`
- `llvmir_attach_library()`
- `llvmir_attach_executable()`


_CAUTION_

Expand Down
31 changes: 31 additions & 0 deletions cmake/LLVMIRUtil.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function(llvmir_attach_bc_target)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

debug(
"@llvmir_attach_bc_target ${DEPENDS_TRGT} linker lang: ${LINKER_LANGUAGE}")
Expand Down Expand Up @@ -170,6 +171,7 @@ function(llvmir_attach_bc_target)
LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
${LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})
endfunction()

#
Expand Down Expand Up @@ -226,6 +228,7 @@ function(llvmir_attach_opt_pass_target)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_BINARY_TYPE}")
message(FATAL_ERROR "Cannot attach ${TRGT} to target of type: \
Expand Down Expand Up @@ -283,6 +286,7 @@ function(llvmir_attach_opt_pass_target)
LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
${LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})
endfunction()

#
Expand Down Expand Up @@ -339,6 +343,7 @@ function(llvmir_attach_disassemble_target)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_BINARY_TYPE}")
message(FATAL_ERROR "Cannot attach ${TRGT} to a ${IN_LLVMIR_TYPE} target.")
Expand Down Expand Up @@ -390,6 +395,7 @@ function(llvmir_attach_disassemble_target)
LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
${LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})
endfunction()

#
Expand Down Expand Up @@ -446,6 +452,7 @@ function(llvmir_attach_assemble_target)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_TEXT_TYPE}")
message(FATAL_ERROR "Cannot attach ${TRGT} to a ${IN_LLVMIR_TYPE} target.")
Expand Down Expand Up @@ -497,6 +504,7 @@ function(llvmir_attach_assemble_target)
LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
${LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})
endfunction()

#
Expand Down Expand Up @@ -553,6 +561,7 @@ function(llvmir_attach_link_target)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_BINARY_TYPE}")
message(FATAL_ERROR "Cannot attach ${TRGT} to a ${IN_LLVMIR_TYPE} target.")
Expand All @@ -568,6 +577,10 @@ function(llvmir_attach_link_target)
endforeach()

set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${TRGT}.${LLVMIR_BINARY_FMT_SUFFIX}")
if(SHORT_NAME)
set(FULL_OUT_LLVMIR_FILE
"${WORK_DIR}/${SHORT_NAME}.${LLVMIR_BINARY_FMT_SUFFIX}")
endif()
get_filename_component(OUT_LLVMIR_FILE ${FULL_OUT_LLVMIR_FILE} NAME)

list(APPEND OUT_LLVMIR_FILES ${OUT_LLVMIR_FILE})
Expand Down Expand Up @@ -595,6 +608,7 @@ function(llvmir_attach_link_target)
LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
${LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})

add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
COMMAND llvm-link
Expand Down Expand Up @@ -660,6 +674,7 @@ function(llvmir_attach_obj_target)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_BINARY_TYPE}")
message(FATAL_ERROR "Cannot attach ${TRGT} to a ${IN_LLVMIR_TYPE} target.")
Expand All @@ -675,6 +690,9 @@ function(llvmir_attach_obj_target)
endforeach()

set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${TRGT}.o")
if(SHORT_NAME)
set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${SHORT_NAME}.o")
endif()
get_filename_component(OUT_LLVMIR_FILE ${FULL_OUT_LLVMIR_FILE} NAME)

list(APPEND OUT_LLVMIR_FILES ${OUT_LLVMIR_FILE})
Expand Down Expand Up @@ -702,6 +720,7 @@ function(llvmir_attach_obj_target)
LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
${LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})

add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
COMMAND llc
Expand Down Expand Up @@ -774,6 +793,7 @@ function(llvmir_attach_executable)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_BINARY_TYPE}" AND
NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_OBJECT_TYPE}")
Expand All @@ -792,6 +812,10 @@ function(llvmir_attach_executable)

add_executable(${TRGT} ${IN_FULL_LLVMIR_FILES})

if(SHORT_NAME)
set_property(TARGET ${TRGT} PROPERTY OUTPUT_NAME ${SHORT_NAME})
endif()

# simply setting the property does not seem to work
#set_property(TARGET ${TRGT}
#PROPERTY INTERFACE_LINK_LIBRARIES ${INTERFACE_LINK_LIBRARIES})
Expand Down Expand Up @@ -824,6 +848,7 @@ function(llvmir_attach_executable)
set_property(TARGET ${TRGT}
PROPERTY LINK_FLAGS_${CMAKE_BUILD_TYPE} ${LINK_FLAGS_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})

# this marks the object as to be linked but not compiled
foreach(IN_FULL_LLVMIR_FILE ${IN_FULL_LLVMIR_FILES})
Expand Down Expand Up @@ -884,6 +909,7 @@ function(llvmir_attach_library)
get_property(LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE}
TARGET ${DEPENDS_TRGT}
PROPERTY LINK_INTERFACE_LIBRARIES_${CMAKE_BUILD_TYPE})
get_property(SHORT_NAME TARGET ${DEPENDS_TRGT} PROPERTY LLVMIR_SHORT_NAME)

if(NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_BINARY_TYPE}" AND
NOT "${IN_LLVMIR_TYPE}" STREQUAL "${LLVMIR_OBJECT_TYPE}")
Expand All @@ -903,6 +929,10 @@ function(llvmir_attach_library)
add_library(${TRGT}
${LLVMIR_ATTACH_UNPARSED_ARGUMENTS} ${IN_FULL_LLVMIR_FILES})

if(SHORT_NAME)
set_property(TARGET ${TRGT} PROPERTY OUTPUT_NAME ${SHORT_NAME})
endif()

# simply setting the property does not seem to work
#set_property(TARGET ${TRGT}
#PROPERTY INTERFACE_LINK_LIBRARIES ${INTERFACE_LINK_LIBRARIES})
Expand Down Expand Up @@ -935,6 +965,7 @@ function(llvmir_attach_library)
set_property(TARGET ${TRGT}
PROPERTY LINK_FLAGS_${CMAKE_BUILD_TYPE} ${LINK_FLAGS_${CMAKE_BUILD_TYPE}})
set_property(TARGET ${TRGT} PROPERTY EXCLUDE_FROM_ALL On)
set_property(TARGET ${TRGT} PROPERTY LLVMIR_SHORT_NAME ${SHORT_NAME})

# this marks the object as to be linked but not compiled
foreach(IN_FULL_LLVMIR_FILE ${IN_FULL_LLVMIR_FILES})
Expand Down

0 comments on commit 152cd9c

Please sign in to comment.