Skip to content

Commit e0f91f0

Browse files
committed
fix issue8: refactor attached target dependencies
1 parent 8ed27a3 commit e0f91f0

File tree

3 files changed

+115
-27
lines changed

3 files changed

+115
-27
lines changed

README.md

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,87 @@
11
# LLVM IR cmake utilities
22

3-
A collection of helper `cmake` functions/macros that eases the generation of
4-
`LLVM` IR and the application of various `LLVM` `opt` passes while obtaining and
5-
preserving the separate IR files that are generated by each user-defined step.
3+
A collection of helper `cmake` functions/macros that eases the generation of `LLVM` IR and the application of various
4+
`LLVM` `opt` passes while obtaining and preserving the separate IR files that are generated by each user-defined step.
5+
6+
7+
## Requirements
8+
9+
- `cmake` 2.8.11 or newer
10+
- `LLVM` tools
11+
Currently used and supported:
12+
- `clang/clang++`
13+
- `opt`
14+
- `llvm-dis` and `llvm-as`
15+
- `llvm-link`
16+
17+
18+
## Installation
19+
20+
- Clone this repo (or even add it as a submodule to your project).
21+
- In your required `CMakeLists.txt` file `include()` the main `cmake` file.
22+
- You are good to go!
23+
24+
25+
## Quick overview
26+
27+
The provided `cmake` commands are expected to work in a parasitic way to targets created via `add_executable()` and
28+
`add_library`. The "gateway" command is `attach_llvmir_bc_target()` which generates the required bitcode files.
29+
Currently, `C/C++` are supported via `clang/clang++`, but in theory any compiler which produces `LLVM` bitcode should be
30+
easily supported (depending how nice it plays with `cmake` too).
31+
32+
The `cmake` calls currently provided are:
33+
34+
- `attach_llvmir_bc_target()`
35+
Attaches to an existing target that can be compiled down to `LLVM IR` and does just that, using all the related flags
36+
and options from the main target. The existing supported targets make use of `clang/clang++`, so currently this means
37+
that the `C/C++` language is supported. It uses `add_custom_library()` `cmake` command under the hood. This creates a
38+
target of type `LLVMIR`.
39+
40+
- `attach_llvmir_opt_pass_target()`
41+
Attaches to a target of type `LLVMIR` and applies various `opt` passes to its bitcode files, specified as arguments.
42+
It uses `add_custom_library()` `cmake` command under the hood. This creates a target of type `LLVMIR`.
43+
44+
- `attach_llvmir_disassemble_target()`
45+
Attaches to a target of type `LLVMIR` and uses `llvm-dis` to disassemble its bitcode files. It uses
46+
`add_custom_library()` `cmake` command under the hood. This creates a target of type `LLVMIR`.
47+
48+
- `attach_llvmir_assemble_target()`
49+
Attaches to a target of type `LLVMIR` and uses `llvm-as` to assemble its bitcode files. It uses `add_custom_library()`
50+
`cmake` command under the hood. This creates a target of type
51+
`LLVMIR`.
52+
53+
- `attach_llvmir_link_target()`
54+
Attaches to a target of type `LLVMIR` and uses `llvm-link` to link its bitcode files to a single bitcode file. The
55+
output bitcode file is names after the target name. It uses `add_custom_library()` `cmake` command under the hood.
56+
This creates a target of type `LLVMIR`.
57+
58+
- `attach_llvmir_library()`
59+
Attaches to a target of type `LLVMIR` and uses the appropriate compiler to compile its bitcode files to a native
60+
library. The output library name uses the target name according to platform rules. It uses `add_library()` `cmake`
61+
command under the hood. This creates a target of type `LLVMIR`.
62+
63+
- `attach_llvmir_executable()`
64+
Attaches to a target of type `LLVMIR` and uses the appropriate compiler to compile its bitcode files to a native
65+
executable. The output library name uses the target name according to platform rules. It uses `add_executable()`
66+
`cmake` command under the hood. This creates a target of type `LLVMIR`.
67+
68+
69+
## Quick How-To
70+
71+
Have a look and toy around with the included examples in this repo. The easiest way to start is:
72+
73+
- `git clone [this repo]`
74+
- `mkdir build && cd build`
75+
- `CC=clang CXX=clang++ cmake [path to example dir]`
76+
- `make`
77+
- `make help` to see the available targets
78+
79+
80+
# Other
81+
82+
## License
83+
84+
GNU LGPL 2.1
85+
86+
687

cmake/LLVM-IR-Util.cmake

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
cmake_minimum_required(VERSION 2.8.11)
88

9-
set(LLVM_IR_UTIL_VERSION_MAJOR "1")
9+
set(LLVM_IR_UTIL_VERSION_MAJOR "2")
1010
set(LLVM_IR_UTIL_VERSION_MINOR "0")
1111
set(LLVM_IR_UTIL_VERSION_PATCH "2")
1212

@@ -283,15 +283,6 @@ LLVMIRSetup()
283283

284284
#
285285

286-
function(attach_llvmir_target OUT_TRGT IN_TRGT)
287-
message(DEPRECATION
288-
"this function is deprecated, use attach_llvmir_bc_target instead")
289-
290-
attach_llvmir_bc_target(${OUT_TRGT} ${IN_TRGT})
291-
endfunction()
292-
293-
#
294-
295286
function(attach_llvmir_bc_target OUT_TRGT IN_TRGT)
296287
## preamble
297288
set(OUT_LLVMIR_FILES "")
@@ -374,8 +365,7 @@ function(attach_llvmir_bc_target OUT_TRGT IN_TRGT)
374365
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_DIR ${WORK_DIR})
375366
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_FILES ${OUT_LLVMIR_FILES})
376367
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
377-
378-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
368+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
379369
endfunction()
380370

381371
#
@@ -433,8 +423,7 @@ function(attach_llvmir_opt_pass_target OUT_TRGT IN_TRGT)
433423
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_DIR ${WORK_DIR})
434424
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_FILES ${OUT_LLVMIR_FILES})
435425
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
436-
437-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
426+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
438427
endfunction()
439428

440429
#
@@ -489,8 +478,7 @@ function(attach_llvmir_disassemble_target OUT_TRGT IN_TRGT)
489478
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_DIR ${WORK_DIR})
490479
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_FILES ${OUT_LLVMIR_FILES})
491480
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
492-
493-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
481+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
494482
endfunction()
495483

496484
#
@@ -545,8 +533,7 @@ function(attach_llvmir_assemble_target OUT_TRGT IN_TRGT)
545533
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_DIR ${WORK_DIR})
546534
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_FILES ${OUT_LLVMIR_FILES})
547535
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
548-
549-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
536+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
550537
endfunction()
551538

552539
#
@@ -589,8 +576,7 @@ function(attach_llvmir_link_target OUT_TRGT IN_TRGT)
589576
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_DIR ${WORK_DIR})
590577
set_property(TARGET ${OUT_TRGT} PROPERTY LLVMIR_FILES ${OUT_LLVMIR_FILES})
591578
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
592-
593-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
579+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
594580

595581
add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
596582
COMMAND llvm-link
@@ -632,14 +618,13 @@ function(attach_llvmir_executable OUT_TRGT IN_TRGT)
632618

633619
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
634620
set_property(TARGET ${OUT_TRGT} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${OUT_DIR})
621+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
635622

636623
# this marks the object as to be linked but not compiled
637624
foreach(IN_FULL_LLVMIR_FILE ${IN_FULL_LLVMIR_FILES})
638625
set_property(SOURCE ${IN_FULL_LLVMIR_FILE} PROPERTY EXTERNAL_OBJECT TRUE)
639626
endforeach()
640627

641-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
642-
643628
## postamble
644629
endfunction()
645630

@@ -671,14 +656,13 @@ function(attach_llvmir_library OUT_TRGT IN_TRGT)
671656

672657
set_property(TARGET ${OUT_TRGT} PROPERTY LINKER_LANGUAGE ${LINKER_LANGUAGE})
673658
set_property(TARGET ${OUT_TRGT} PROPERTY LIBRARY_OUTPUT_DIRECTORY ${OUT_DIR})
659+
set_property(TARGET ${OUT_TRGT} PROPERTY EXCLUDE_FROM_ALL On)
674660

675661
# this marks the object as to be linked but not compiled
676662
foreach(IN_FULL_LLVMIR_FILE ${IN_FULL_LLVMIR_FILES})
677663
set_property(SOURCE ${IN_FULL_LLVMIR_FILE} PROPERTY EXTERNAL_OBJECT TRUE)
678664
endforeach()
679665

680-
add_dependencies(${IN_TRGT} ${OUT_TRGT})
681-
682666
## postamble
683667
endfunction()
684668

example01/src/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,44 @@ target_include_directories(qux PUBLIC
4747
# attachments on first target
4848

4949
attach_llvmir_bc_target(qux_bc qux)
50+
add_dependencies(qux_bc qux)
5051

5152
attach_llvmir_opt_pass_target(qux_pass1 qux_bc -mem2reg)
53+
add_dependencies(qux_pass1 qux_bc)
54+
5255
attach_llvmir_opt_pass_target(qux_pass2 qux_pass1 -simplifycfg -licm)
56+
add_dependencies(qux_pass2 qux_pass1)
57+
5358
attach_llvmir_opt_pass_target(qux_pass3 qux_pass2 -licm)
59+
add_dependencies(qux_pass3 qux_pass2)
5460

5561
attach_llvmir_disassemble_target(qux_dis qux_pass2)
62+
add_dependencies(qux_dis qux_pass2)
63+
5664
attach_llvmir_assemble_target(qux_as qux_dis)
65+
add_dependencies(qux_as qux_dis)
5766

5867
attach_llvmir_link_target(qux_llvmlink qux_pass2)
68+
add_dependencies(qux_llvmlink qux_pass2)
5969

6070
attach_llvmir_opt_pass_target(qux_pass4 qux_llvmlink -simplifycfg)
71+
add_dependencies(qux_pass4 qux_llvmlink)
6172

6273
attach_llvmir_executable(qux_bc_exe qux_pass4)
74+
add_dependencies(qux_bc_exe qux_pass4)
75+
6376
attach_llvmir_executable(qux_bc_exe2 qux_pass2)
77+
add_dependencies(qux_bc_exe2 qux_pass2)
6478

6579
attach_llvmir_library(qux_bc_lib qux_pass2 SHARED)
80+
add_dependencies(qux_bc_lib qux_pass2)
81+
82+
83+
add_custom_target(qux_pipeline1 DEPENDS
84+
qux_pass1)
85+
86+
add_custom_target(qux_pipeline2 DEPENDS
87+
qux_pass2)
6688

89+
set_property(TARGET qux_pipeline2 PROPERTY EXCLUDE_FROM_ALL Off)
6790

0 commit comments

Comments
 (0)