This is an archive of the discontinued LLVM Phabricator instance.

cmake: Avoid leading space in LLVM_DEFINITIONS.
ClosedPublic

Authored by chfast on Oct 5 2015, 4:33 AM.

Details

Summary

Unnecessary space at the beginning of LLVM_DEFINITIONS in cmake shared files can break projects that use the variable.

Diff Detail

Repository
rL LLVM

Event Timeline

chfast updated this revision to Diff 36502.Oct 5 2015, 4:33 AM
chfast retitled this revision from to cmake: Avoid leading space in LLVM_DEFINITIONS..
chfast updated this object.
chfast added a subscriber: llvm-commits.
chapuni added a subscriber: chapuni.Oct 5 2015, 4:49 AM

I guess it would work. By the way, what is your issue?

I think LLVM_DEFINITIONS may be list-ized.

chfast added a comment.Oct 5 2015, 4:51 AM

I'm getting a compiler command like clang++ -D -D_GNU_SOURCE.

I guess it would work. By the way, what is your issue?

It appears that when there's a leading space in LLVM_DEFINITIONS that

add_definitions(${LLVM_DEFINITIONS}

works as expected, but

target_compile_definitions(example PUBLIC ${LLVM_DEFINITIONS})

gets you -D -DWHATEVER. Removing the leading space in LLVM_DEFINITIONS seems to make target_compile_definitions behave as expected.

chfast added a comment.Oct 6 2015, 4:04 AM

Alternative solution is to keep LLVM_DEFINITIONS as a cmake lists (; separated values). And convert it into the flags string on demand. E.g. with a helper function like llvm_get_definitions.

Alternative solution is to keep LLVM_DEFINITIONS as a cmake lists (; separated values). And convert it into the flags string on demand. E.g. with a helper function like llvm_get_definitions.

That should be the preferred solution, as this matches both the correct use of add_definitions and target_compile_definitions.

Also, have you considered replacing add_llvm_definitions by proper use of target_compile_definitions? That would make LLVM_DEFINITONS unnecessary.

chfast added a comment.Oct 6 2015, 4:24 AM

I agree target_compile_definitions and others are super nice. But that transition would probably take some time and might break many things. I'm also not sure that information will be preserved by exported targets. However I can start working on that as a separated change after broader acceptance.

This change is a quick fix. Not very beautiful but helps in my case.

I agree target_compile_definitions and others are super nice. But that transition would probably take some time and might break many things. I'm also not sure that information will be preserved by exported targets. However I can start working on that as a separated change after broader acceptance.

This change is a quick fix. Not very beautiful but helps in my case.

Does turning it back into a list break anything? If not the "quick fix" might be as easy as removing the quotes from the set line.

chfast added a comment.Oct 6 2015, 4:42 AM

Changing LLVM_DEFINITIONS back to list needs also changes in other places and would probably break third party projects that use the variable.

Having had another look at how add_llvm_definitions is used, it is broken for target_compile_definitions anyways as LLVM_DEFINITIONS can contain compile options as well (something like -m32 would be converted to -D-m32, which is illegal, if passed to target_compile_definitions).

beanz added a subscriber: beanz.Oct 7 2015, 10:05 AM

I haven't tested it yet, but I suspect something like this would work by treating LLVM_DEFINITONS as a list:

diff --git a/cmake/modules/AddLLVMDefinitions.cmake b/cmake/modules/AddLLVMDefinitions.cmake
index 33ac973..c4d3cc8 100644
--- a/cmake/modules/AddLLVMDefinitions.cmake
+++ b/cmake/modules/AddLLVMDefinitions.cmake
@@ -6,8 +6,6 @@
 
 macro(add_llvm_definitions)
   # We don't want no semicolons on LLVM_DEFINITIONS:
-  foreach(arg ${ARGN})
-    set(LLVM_DEFINITIONS "${LLVM_DEFINITIONS} ${arg}")
-  endforeach(arg)
+  list(APPEND LLVM_DEFINITIONS ${ARGN})
   add_definitions( ${ARGN} )
 endmacro(add_llvm_definitions)
diff --git a/cmake/modules/LLVMConfig.cmake.in b/cmake/modules/LLVMConfig.cmake.in
index 6855c44..a71d8d8 100644
--- a/cmake/modules/LLVMConfig.cmake.in
+++ b/cmake/modules/LLVMConfig.cmake.in
@@ -49,7 +49,7 @@ set(LLVM_LIBDIR_SUFFIX @LLVM_LIBDIR_SUFFIX@)
 
 set(LLVM_INCLUDE_DIRS "@LLVM_CONFIG_INCLUDE_DIRS@")
 set(LLVM_LIBRARY_DIRS "@LLVM_CONFIG_LIBRARY_DIRS@")
-set(LLVM_DEFINITIONS "@LLVM_DEFINITIONS@")
+set(LLVM_DEFINITIONS @LLVM_DEFINITIONS@)
 set(LLVM_CMAKE_DIR "@LLVM_CONFIG_CMAKE_DIR@")
 set(LLVM_TOOLS_BINARY_DIR "@LLVM_CONFIG_TOOLS_BINARY_DIR@")
chfast added a comment.Oct 7 2015, 1:52 PM

That does not work. You will get set(LLVM_DEFINITIONS -D_GNU_SOURCE;-D__STDC_CONSTANT_MACROS;-D__STDC_FORMAT_MACROS;-D__STDC_LIMIT_MACROS)
and llvm-config --cppflags: -D_GNU_SOURCE;-D__STDC_CONSTANT_MACROS;-D__STDC_FORMAT_MACROS;-D__STDC_LIMIT_MACROS.

chapuni accepted this revision.Oct 7 2015, 4:20 PM
chapuni added a reviewer: chapuni.

For now, I think this may be fed to trunk as *quick fix*, as far as we shall be working on this area.

This revision is now accepted and ready to land.Oct 7 2015, 4:20 PM
chfast added a comment.Oct 8 2015, 5:32 AM

I will land the change today. Any objections? Someone else would like to send an approval?

This revision was automatically updated to reflect the committed changes.