This is an archive of the discontinued LLVM Phabricator instance.

[CMake] Make the bindir and libdir arguments to set_output_directory optional
ClosedPublic

Authored by john.brawn on Sep 28 2015, 8:58 AM.

Details

Summary

When building a plugin against an installed LLVM toolchain using add_llvm_loadable_module (in the documented manner) doesn't work as nothing sets the *_OUTPUT_INTDIR variables causing an error when set_output_directory is called. Making those arguments optional (causing the default output directory to be used) fixes this.

Diff Detail

Repository
rL LLVM

Event Timeline

john.brawn updated this revision to Diff 35877.Sep 28 2015, 8:58 AM
john.brawn retitled this revision from to [CMake] Make the bindir and libdir arguments to set_output_directory optional.
john.brawn updated this object.
john.brawn added reviewers: chapuni, chandlerc, beanz.
john.brawn set the repository for this revision to rL LLVM.
john.brawn added a subscriber: llvm-commits.
beanz added inline comments.Sep 28 2015, 10:46 AM
cmake/modules/AddLLVM.cmake
206 ↗(On Diff #35877)

I think that a better way to do this would be to use cmake_parse_arguments and make output and bind directory be single value options. Then if one isn't specified it is ignored.

I also think it would be cleaner if set_target_properties were only called if the corresponding value is set.

chapuni accepted this revision.Sep 28 2015, 5:32 PM
chapuni edited edge metadata.

LGTM, as a step.

That said, I think set_output_directory may be rewritten.
It may be dedicated to set outdir along LLVM_*_OUTPUT_INTDIR, or reset outdir.

This revision is now accepted and ready to land.Sep 28 2015, 5:32 PM
john.brawn added inline comments.Sep 29 2015, 5:18 AM
cmake/modules/AddLLVM.cmake
206 ↗(On Diff #35877)

I had thought of using cmake_parse_arguments, but decided against it as it would involve changing how set_output_directory is called. It's only called in three places though, so actually that won't be too much trouble. I'll give it a go.

john.brawn updated this revision to Diff 35980.Sep 29 2015, 8:14 AM
john.brawn edited edge metadata.

I've gone with the suggestion of using cmake_parse_arguments. I haven't gone for calling set_target_properties is the argument is set, instead of the check at the start, mainly as I'm not sure how moddir would be handled if that were done.

beanz edited edge metadata.Sep 29 2015, 9:41 AM

I think all you need to do for handling unset values is something like this:

diff --git a/cmake/modules/AddLLVM.cmake b/cmake/modules/AddLLVM.cmake
index 993bc35..63047e6 100644
--- a/cmake/modules/AddLLVM.cmake
+++ b/cmake/modules/AddLLVM.cmake
@@ -211,17 +211,33 @@ function(set_output_directory target)
   if(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
     foreach(build_mode ${CMAKE_CONFIGURATION_TYPES})
       string(TOUPPER "${build_mode}" CONFIG_SUFFIX)
-      string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINDIR})
-      string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBDIR})
-      string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
-      set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
-      set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
-      set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
+      if(ARG_BINDIR)
+        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} bi ${ARG_BINDIR})
+        set_target_properties(${target} PROPERTIES "RUNTIME_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${bi})
+      endif()
+
+      if(ARG_LIBDIR)
+        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} li ${ARG_LIBDIR})
+        set_target_properties(${target} PROPERTIES "ARCHIVE_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${li})
+      endif()
+
+      if(moddir)
+        string(REPLACE ${CMAKE_CFG_INTDIR} ${build_mode} mi ${moddir})
+        set_target_properties(${target} PROPERTIES "LIBRARY_OUTPUT_DIRECTORY_${CONFIG_SUFFIX}" ${mi})
+      endif()
     endforeach()
   else()
-    set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINDIR})
-    set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBDIR})
-    set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
+    if(ARG_BINDIR)
+      set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${ARG_BINDIR})
+    endif()
+
+    if(ARG_LIBDIR)
+      set_target_properties(${target} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${ARG_LIBDIR})
+    endif()
+
+    if(moddir)
+      set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${moddir})
+    endif()
   endif()
 endfunction()

I have one other comment below.

Thanks,
-Chris

cmake/modules/AddLLVM.cmake
197 ↗(On Diff #35980)

Two things here.

  1. You need another empty string before ${ARGN} otherwise the first argument from ARGN will be treated as your multi-variable option list.
  1. Maybe make the options more explicitly worded now that they are externally facing. I'd suggest BINARY_DIR and LIBRARY_DIR.
john.brawn updated this revision to Diff 36105.Sep 30 2015, 8:00 AM
john.brawn edited edge metadata.

Made the suggested changes to the handling of unset arguments.

beanz accepted this revision.Sep 30 2015, 8:09 AM
beanz edited edge metadata.

LGTM. Thanks!

-Chris

This revision was automatically updated to reflect the committed changes.