Skip to content

Commit 66fed3c

Browse files
committed
fix for issue6
1 parent 3b082d7 commit 66fed3c

File tree

10 files changed

+256
-106
lines changed

10 files changed

+256
-106
lines changed

cmake/LLVM-IR-Util.cmake

Lines changed: 185 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 2.8.11)
88

99
set(LLVM_IR_UTIL_VERSION_MAJOR "1")
1010
set(LLVM_IR_UTIL_VERSION_MINOR "0")
11-
set(LLVM_IR_UTIL_VERSION_PATCH "1")
11+
set(LLVM_IR_UTIL_VERSION_PATCH "2")
1212

1313
string(CONCAT LLVM_IR_UTIL_VERSION
1414
${LLVM_IR_UTIL_VERSION_MAJOR} "."
@@ -57,6 +57,21 @@ function(fatal message_txt)
5757
endfunction()
5858

5959

60+
function(debug message_txt)
61+
if($ENV{LLVMIR_CMAKE_DEBUG})
62+
message(STATUS "[DEBUG] ${message_txt}")
63+
endif()
64+
endfunction()
65+
66+
67+
macro(catuniq lst)
68+
list(APPEND ${lst} ${ARGN})
69+
if(${lst})
70+
list(REMOVE_DUPLICATES ${lst})
71+
endif()
72+
endmacro()
73+
74+
6075
macro(SetLLVMIRCompiler linker_language)
6176
if("${LLVMIR_COMPILER}" STREQUAL "")
6277
set(LLVMIR_COMPILER ${CMAKE_${linker_language}_COMPILER})
@@ -113,6 +128,155 @@ function(CheckLLVMIRTargetProperties trgt)
113128
endfunction()
114129

115130

131+
function(ExtractCompileDefsProperties out_compile_defs from)
132+
set(defs "")
133+
set(compile_defs "")
134+
set(prop_name "COMPILE_DEFINITIONS")
135+
136+
# per directory
137+
get_property(defs DIRECTORY PROPERTY ${prop_name})
138+
foreach(def ${defs})
139+
list(APPEND compile_defs -D${def})
140+
endforeach()
141+
142+
get_property(defs DIRECTORY PROPERTY ${prop_name}_${CMAKE_BUILD_TYPE})
143+
foreach(def ${defs})
144+
list(APPEND compile_defs -D${def})
145+
endforeach()
146+
147+
# per target
148+
if(TARGET ${from})
149+
get_property(defs TARGET ${from} PROPERTY ${prop_name})
150+
foreach(def ${defs})
151+
list(APPEND compile_defs -D${def})
152+
endforeach()
153+
154+
get_property(defs TARGET ${from} PROPERTY ${prop_name}_${CMAKE_BUILD_TYPE})
155+
foreach(def ${defs})
156+
list(APPEND compile_defs -D${def})
157+
endforeach()
158+
159+
get_property(defs TARGET ${from} PROPERTY INTERFACE_${prop_name})
160+
foreach(def ${defs})
161+
list(APPEND compile_defs -D${def})
162+
endforeach()
163+
else()
164+
# per file
165+
get_property(defs SOURCE ${from} PROPERTY ${prop_name})
166+
foreach(def ${defs})
167+
list(APPEND compile_defs -D${def})
168+
endforeach()
169+
170+
get_property(defs SOURCE ${from} PROPERTY ${prop_name}_${CMAKE_BUILD_TYPE})
171+
foreach(def ${defs})
172+
list(APPEND compile_defs -D${def})
173+
endforeach()
174+
endif()
175+
176+
list(REMOVE_DUPLICATES compile_defs)
177+
178+
debug("@ExtractCompileDefsProperties ${from}: ${compile_defs}")
179+
180+
set(${out_compile_defs} ${compile_defs} PARENT_SCOPE)
181+
endfunction()
182+
183+
184+
function(ExtractCompileOptionProperties out_compile_options trgt)
185+
set(options "")
186+
set(compile_options "")
187+
set(prop_name "COMPILE_OPTIONS")
188+
189+
# per directory
190+
get_property(options DIRECTORY PROPERTY ${prop_name})
191+
foreach(opt ${options})
192+
list(APPEND compile_options ${opt})
193+
endforeach()
194+
195+
# per target
196+
get_property(options TARGET ${trgt} PROPERTY ${prop_name})
197+
foreach(opt ${options})
198+
list(APPEND compile_options ${opt})
199+
endforeach()
200+
201+
get_property(options TARGET ${trgt} PROPERTY INTERFACE_${prop_name})
202+
foreach(opt ${options})
203+
list(APPEND compile_options ${opt})
204+
endforeach()
205+
206+
list(REMOVE_DUPLICATES compile_options)
207+
208+
debug("@ExtractCompileOptionProperties ${trgt}: ${compile_options}")
209+
210+
set(${out_compile_options} ${compile_options} PARENT_SCOPE)
211+
endfunction()
212+
213+
214+
function(ExtractIncludeDirsProperties out_include_dirs trgt)
215+
set(dirs "")
216+
set(prop_name "INCLUDE_DIRECTORIES")
217+
218+
# per directory
219+
get_property(dirs DIRECTORY PROPERTY ${prop_name})
220+
foreach(dir ${dirs})
221+
list(APPEND include_dirs -I${dir})
222+
endforeach()
223+
224+
# per target
225+
get_property(dirs TARGET ${trgt} PROPERTY ${prop_name})
226+
foreach(dir ${dirs})
227+
list(APPEND include_dirs -I${dir})
228+
endforeach()
229+
230+
get_property(dirs TARGET ${trgt} PROPERTY INTERFACE_${prop_name})
231+
foreach(dir ${dirs})
232+
list(APPEND include_dirs -I${dir})
233+
endforeach()
234+
235+
get_property(dirs TARGET ${trgt} PROPERTY INTERFACE_SYSTEM_${prop_name})
236+
foreach(dir ${dirs})
237+
list(APPEND include_dirs -I${dir})
238+
endforeach()
239+
240+
list(REMOVE_DUPLICATES include_dirs)
241+
242+
debug("@ExtractIncludeDirsProperties ${trgt}: ${include_dirs}")
243+
244+
set(${out_include_dirs} ${include_dirs} PARENT_SCOPE)
245+
endfunction()
246+
247+
248+
function(ExtractLangFlags out_lang_flags lang)
249+
set(lang_flags "")
250+
251+
list(APPEND lang_flags ${CMAKE_${lang}_FLAGS_${CMAKE_BUILD_TYPE}})
252+
list(APPEND lang_flags ${CMAKE_${lang}_FLAGS})
253+
254+
list(REMOVE_DUPLICATES lang_flags)
255+
256+
debug("@ExtractLangFlags ${lang}: ${lang_flags}")
257+
258+
set(${out_lang_flags} ${out_lang_flags_tmp} PARENT_SCOPE)
259+
endfunction()
260+
261+
262+
function(ExtractCompileFlags out_compile_flags from)
263+
#message(DEPRECATION "COMPILE_FLAGS property is deprecated.")
264+
265+
set(compile_flags "")
266+
set(prop_name "COMPILE_FLAGS")
267+
268+
# deprecated according to cmake docs
269+
if(TARGET ${from})
270+
get_property(compile_flags TARGET ${from} PROPERTY ${prop_name})
271+
else()
272+
get_property(compile_flags SOURCE ${from} PROPERTY ${prop_name})
273+
endif()
274+
275+
debug("@ExtractCompileFlags ${from}: ${compile_flags}")
276+
277+
set(${out_compile_flags} ${compile_flags} PARENT_SCOPE)
278+
endfunction()
279+
116280
#
117281

118282
LLVMIRSetup()
@@ -139,83 +303,28 @@ function(attach_llvmir_bc_target OUT_TRGT IN_TRGT)
139303
get_property(IN_FILES TARGET ${IN_TRGT} PROPERTY SOURCES)
140304
get_property(LINKER_LANGUAGE TARGET ${IN_TRGT} PROPERTY LINKER_LANGUAGE)
141305

306+
debug("@attach_llvmir_bc_target ${IN_TRGT} linker lang: ${LINKER_LANGUAGE}")
307+
142308
SetLLVMIRCompiler(${LINKER_LANGUAGE})
143309

144310
## command options
145311
set(WORK_DIR "${CMAKE_CURRENT_BINARY_DIR}/${LLVMIR_DIR}/${OUT_TRGT}")
146312
file(MAKE_DIRECTORY ${WORK_DIR})
147313

148314
# compile definitions
149-
set(SRC_DEFS "")
150-
151-
# per directory
152-
get_property(SRC_DEFS_TMP DIRECTORY PROPERTY COMPILE_DEFINITIONS)
153-
foreach(DEF ${SRC_DEFS_TMP})
154-
list(APPEND SRC_DEFS -D${DEF})
155-
endforeach()
156-
157-
get_property(SRC_DEFS_TMP DIRECTORY PROPERTY
158-
COMPILE_DEFINITIONS_${CMAKE_BUILD_TYPE})
159-
foreach(DEF ${SRC_DEFS_TMP})
160-
list(APPEND SRC_DEFS -D${DEF})
161-
endforeach()
162-
163-
# per target
164-
get_property(SRC_DEFS_TMP TARGET ${IN_TRGT} PROPERTY COMPILE_DEFINITIONS)
165-
foreach(DEF ${SRC_DEFS_TMP})
166-
list(APPEND SRC_DEFS -D${DEF})
167-
endforeach()
168-
169-
get_property(SRC_DEFS_TMP TARGET ${IN_TRGT} PROPERTY
170-
COMPILE_DEFINITIONS_${CMAKE_BUILD_TYPE})
171-
foreach(DEF ${SRC_DEFS_TMP})
172-
list(APPEND SRC_DEFS -D${DEF})
173-
endforeach()
315+
ExtractCompileDefsProperties(IN_DEFS ${IN_TRGT})
174316

175-
get_property(SRC_DEFS_TMP TARGET ${IN_TRGT} PROPERTY
176-
INTERFACE_COMPILE_DEFINITIONS)
177-
foreach(DEF ${SRC_DEFS_TMP})
178-
list(APPEND SRC_DEFS -D${DEF})
179-
endforeach()
180-
181-
list(REMOVE_DUPLICATES SRC_DEFS)
317+
# includes
318+
ExtractIncludeDirsProperties(IN_INCLUDES ${IN_TRGT})
182319

183320
# compile options
184-
set(SRC_COMPILE_OPTIONS "")
185-
get_property(SRC_COMPILE_OPTIONS TARGET ${IN_TRGT} PROPERTY COMPILE_OPTIONS)
186-
187-
# compile lang flags
188-
set(SRC_LANG_FLAGS_TMP ${CMAKE_${LINKER_LANGUAGE}_FLAGS_${CMAKE_BUILD_TYPE}})
189-
if(SRC_LANG_FLAGS_TMP STREQUAL "")
190-
set(SRC_LANG_FLAGS_TMP ${CMAKE_${LINKER_LANGUAGE}_FLAGS})
191-
endif()
192-
193-
# this transforms the string to a list and gets rid of the double quotes
194-
# when assembling the command arguments
195-
string(REPLACE " " ";" SRC_LANG_FLAGS "${SRC_LANG_FLAGS_TMP}")
321+
ExtractCompileOptionProperties(IN_COMPILE_OPTIONS ${IN_TRGT})
196322

197323
# compile flags
198-
# deprecated according to cmake docs
199-
get_property(SRC_FLAGS_TMP TARGET ${IN_TRGT} PROPERTY COMPILE_FLAGS)
200-
201-
# this transforms the string to a list and gets rid of the double quotes
202-
# when assembling the command arguments
203-
string(REPLACE " " ";" SRC_FLAGS "${SRC_FLAGS_TMP}")
324+
ExtractCompileFlags(IN_COMPILE_FLAGS ${IN_TRGT})
204325

205-
# includes
206-
set(SRC_INCLUDES "")
207-
208-
get_property(INC_DIRS TARGET ${IN_TRGT} PROPERTY INCLUDE_DIRECTORIES)
209-
foreach(DIR ${INC_DIRS})
210-
list(APPEND SRC_INCLUDES -I${DIR})
211-
endforeach()
212-
213-
get_property(INC_DIRS TARGET ${IN_TRGT} PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
214-
foreach(DIR ${INC_DIRS})
215-
list(APPEND SRC_INCLUDES -I${DIR})
216-
endforeach()
217-
218-
list(REMOVE_DUPLICATES SRC_INCLUDES)
326+
# compile lang flags
327+
ExtractLangFlags(IN_LANG_FLAGS ${LINKER_LANGUAGE})
219328

220329
## main operations
221330
foreach(IN_FILE ${IN_FILES})
@@ -225,29 +334,21 @@ function(attach_llvmir_bc_target OUT_TRGT IN_TRGT)
225334
set(FULL_OUT_LLVMIR_FILE "${WORK_DIR}/${OUT_LLVMIR_FILE}")
226335

227336
# compile definitions per source file
228-
set(SRC_FILE_DEFS "")
229-
230-
get_property(SRC_DEFS_TMP SOURCE ${INFILE} PROPERTY COMPILE_DEFINITIONS)
231-
foreach(DEF ${SRC_DEFS_TMP})
232-
list(APPEND SRC_FILE_DEFS -D${DEF})
233-
endforeach()
234-
235-
get_property(SRC_DEFS_TMP SOURCE ${IN_TRGT} PROPERTY
236-
COMPILE_DEFINITIONS_${CMAKE_BUILD_TYPE})
237-
foreach(DEF ${SRC_DEFS_TMP})
238-
list(APPEND SRC_FILE_DEFS -D${DEF})
239-
endforeach()
337+
ExtractCompileDefsProperties(IN_FILE_DEFS ${IN_FILE})
240338

241339
# compile flags per source file
242-
get_property(SRC_FLAGS_TMP SOURCE ${INFILE} PROPERTY COMPILE_FLAGS)
243-
244-
# this transforms the string to a list and gets rid of the double quotes
245-
# when assembling the command arguments
246-
string(REPLACE " " ";" SRC_FILE_FLAGS "${SRC_FLAGS_TMP}")
340+
ExtractCompileFlags(IN_FILE_COMPILE_FLAGS ${IN_FILE})
247341

248342
# stitch all args together
249-
set(CMD_ARGS -emit-llvm ${SRC_LANG_FLAGS} ${SRC_FLAGS} ${SRC_COMPILE_OPTIONS}
250-
${SRC_FILE_FLAGS} ${SRC_FILE_DEFS} ${SRC_DEFS} ${SRC_INCLUDES})
343+
catuniq(CURRENT_DEFS ${IN_DEFS} ${IN_FILE_DEFS})
344+
debug("@attach_llvmir_bc_target ${IN_TRGT} defs: ${CURRENT_DEFS}")
345+
346+
catuniq(CURRENT_COMPILE_FLAGS ${IN_COMPILE_FLAGS} ${IN_FILE_COMPILE_FLAGS})
347+
debug("@attach_llvmir_bc_target ${IN_TRGT} compile flags: \
348+
${CURRENT_COMPILE_FLAGS}")
349+
350+
set(CMD_ARGS "-emit-llvm" ${IN_LANG_FLAGS} ${IN_COMPILE_OPTIONS}
351+
${CURRENT_COMPILE_FLAGS} ${CURRENT_DEFS} ${IN_INCLUDES})
251352

252353
add_custom_command(OUTPUT ${FULL_OUT_LLVMIR_FILE}
253354
COMMAND ${LLVMIR_COMPILER}

example/include/bar.hpp

Lines changed: 0 additions & 5 deletions
This file was deleted.

example/src/foo.cpp

Lines changed: 0 additions & 13 deletions
This file was deleted.
File renamed without changes.

example01/include/bar.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
#ifndef DIR_FOO
4+
#error "DIR_FOO is not defined"
5+
#endif
6+
7+
void foo();
8+

example01/include/detail/bar_impl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
namespace detail {
4+
const int g_bar_count = 0;
5+
}
6+

example01/include/general/general.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#pragma once
3+
4+
#include <iostream>
5+
6+
7+
void general(void) {
8+
std::cout << "this is general\n";
9+
10+
return;
11+
}

0 commit comments

Comments
 (0)