3

Full debug compilation requires too much memory. My machine is only 4GB of memory. I would like to debug a module using debug compiler, other modules with release compiling.

In general, I build LLVM in debug mode or release mode using the following commands:

cmake /home/llvm380 -DCMAKE_BUILD_TYPE="Release" 

cmake /home/llvm380 -DCMAKE_BUILD_TYPE="Debug" 

The choice of a build type has impact on a whole build process. Full debug build is not convenient to debug as it needs about 13GB memory. I only want to debug a small part of a code.

Example: I want to make llvm/lib/IR module debug build. How to modify the CMakeLists.txt in llvm\lib\IR?

Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
freyone
  • 349
  • 1
  • 8
  • Doubt this is possible without much hacking. – arrowd Mar 19 '17 at 07:34
  • It is probably not possible without hacking on LLVM's CMakeLists files. But it is definitely possible if you are ready to hack on them. – Stanislav Pankevich Mar 19 '17 at 12:16
  • 1
    I don't understand why this question is on hold. Seems like the voters are completely out of the context, since the question is absolutely clear and valid. – AlexDenisov Mar 19 '17 at 19:16
  • @freyone, your question is very clear to me but I can see how it can confuse the folks who voted for this question to be closed as "too broad". Please fill in a bit more details like that you are building LLVM from source and want to debug its specific module etc. – Stanislav Pankevich Mar 19 '17 at 19:17

1 Answers1

2

LLVM works with CMake, so to enable debug mode on specific modules you either need to enable -g flag on particular files or enable it on the whole build targets. Most likely you will need:

COMPILE_FLAGS or COMPILE_DEFITIONS target property (see Difference between COMPILE_FLAGS and COMPILE_OPTIONS) or COMPILE_FLAGS source file property.

I have tried this quickly on one project, on both file- and target-levels and can confirm that it is doable.

Let's assume you have one file MySourceFile.cpp and a build target MyTarget. Below I am adding -hack-target and -hack-file to better see where this results in.

set(my_source_files
  MySourceFile.cpp
)

add_library(MyTarget SHARED ${my_source_files} ...)

Adding -g flag on a target level:

get_target_property(my_target_compile_flags MyTarget COMPILE_FLAGS)
if (NOT my_target_compile_flags)
  set(my_target_compile_flags "")
endif()
set_target_properties(MyTarget
                      PROPERTIES COMPILE_OPTIONS  
                      "${my_target_compile_options} -g -hack-target")

Adding -g flag on a source file level:

get_source_file_property(my_source_file_compile_flags
                         MySource.cpp
                         COMPILE_FLAGS)

if (NOT my_source_file_compile_flags)
  set(my_source_file_compile_flags "")
endif()
set_source_files_properties(MySource.cpp
                            PROPERTIES
                            COMPILE_FLAGS
                            "${my_source_file_compile_flags} -g -hack-file")

From the above lines the CMake helper functions can be made for your convenience of enabling -g flag on the stuff you need easier.


I can speak of how this works on Ninja and Xcode:

Ninja

$ grep -ri "hack-" BuildNinja

BuildNinja//build.ninja:  FLAGS = -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -fPIC    -g -hack-target -std=c++11  -g -hack-file

Xcode

In Xcode -g of a target level goes to "Generate Debug Symbols" property:

Xcode target with -g flag enabled in Release Xcode target without -g flag enabled in Release

This is how flags look like:

Target one: enter image description here

File one: enter image description here

Community
  • 1
  • 1
Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
  • according you suggestion.i modified Cmakelists.txt in my modules. after then,how to cmake? still "cmake /home/llvm380 -DCMAKE_BUILD_TYPE="Release" " need to add "-g"? – freyone Mar 22 '17 at 06:32
  • No, you only add `-g` in the CMakeLists as I showed. No need to pass it anywhere else. – Stanislav Pankevich Mar 22 '17 at 06:38
  • And yes, you have to run your `cmake ...` command again. Feel free to use CMake's `message` command to debug your CMakeLists files. Example: `message(STATUS "This is debug string you will see when you run cmake")`. See also http://stackoverflow.com/a/5403778/598057. – Stanislav Pankevich Mar 22 '17 at 07:20
  • Did you mean to get_target_property of COMPILE_OPTIONS instead of COMPILE_FLAGS? – Peter Tseng Oct 09 '17 at 23:03
  • I think I posted the working code at the time of my answer. You probably know that they are the same thing: https://stackoverflow.com/questions/24302073/difference-between-compile-flags-and-compile-options, right. – Stanislav Pankevich Oct 10 '17 at 07:57