Build clang front end only for few languages ( say C and C++ )

Hi,

I am trying to build clang, however the build size is quite large. As clang supports non-C family languages as well ( e.g. Java, Fortran ), is there a way to turn that off during the build. I just want to have support for C and C++ and don’t care about other languages.

Is there a CMake option that needs to be set to do that??

Thanks a lot!

Best Regards,

Nitish

  • llvm-dev, + cfe-dev (moving off llvm-dev)

Hi Nitish,

Hi,

I am trying to build clang, however the build size is quite large. As clang supports non-C family languages as well ( e.g. Java, Fortran ),

Clang does not accept Java or Fortran code. It links llvm, which has some language-specific support for calling conventions, but excising this support might not save you much in terms of binary size.

is there a way to turn that off during the build. I just want to have support for C and C++ and don’t care about other languages.

Is there a CMake option that needs to be set to do that??

What are you measuring exactly? Is it the size of the build directory, the size of the clang binary itself, or something else?

How small does clang need to be for your purposes?

vedant

Hi Vedant,

Clang + llvm build together takes around 26 GB. I was curious if there are any tricks to reduce the size as I need only C and C++ languages in frontend and RISCV ISA for llvm backend. For the backend I am already passing -DLLVM_TARGETS_TO_BUILD=RISCV to generate the backend only for RISCV. I was curious if I can do something to reduce the size of the clang build.

Thanks,

Best Regards,
Nitish

Hi Vedant,

Clang + llvm build together takes around 26 GB. I was curious if there are any tricks to reduce the size as I need only C and C++ languages in frontend and RISCV ISA for llvm backend. For the backend I am already passing -DLLVM_TARGETS_TO_BUILD=RISCV to generate the backend only for RISCV. I was curious if I can do something to reduce the size of the clang build.

Some ideas:

  1. It sounds like you might have configured a Debug build. If you don’t need to debug clang, you can configure a Release build instead, which will be much smaller.

  2. You can enable a shared-library build to cut down on binary size (-DBUILD_SHARED_LIBS=On). That means there will be just one copy of the code from each llvm library in your build directory, instead of two.

  3. You can pare down the list of targets you build. Just building the “clang” target is much faster, and takes much less space, than building the full suite of llvm tools (which is what you get if you run “ninja” or “make”).

  4. If you’re building a Release clang, you can use -Os or -Oz, instead of -O3 which is the default.

vedant

Hi Vedant,

I have tried that. Choosing Release build and MinSizeRel does reduce the size, and I have also tried to set static linking off. As, I am currently hacking clang and llvm both, I do need to have the debug metadata attached to the clang and llvm binaries, that’s why I was asking if there are other methods to reduce the size.

Thanks,

Best Regards,
Nitish

Hi Vedant,

  Clang + llvm build together takes around 26 GB. I was curious if there are any tricks to reduce the size as I need only C and C++ languages in frontend and RISCV ISA for llvm backend. For the backend I am already passing -DLLVM_TARGETS_TO_BUILD=RISCV to generate the backend only for RISCV. I was curious if I can do something to reduce the size of the clang build.

Some ideas:

1. It sounds like you might have configured a Debug build. If you don't need to debug clang, you can configure a Release build instead, which will be much smaller.

2. You can enable a shared-library build to cut down on binary size (-DBUILD_SHARED_LIBS=On). That means there will be just one copy of the code from each llvm library in your build directory, instead of two.

Please don’t do this. If you want to reduce code duplication the option you want is -DLLVM_LINK_LLVM_DYLIB=On. BUILD_SHARED_LIBS is not well supported and only for use by developers working on LLVM & Clang. If you’re intending to build something usable but are trying to be conscious of the size using libLLVM is the right way to go.

-Chris

:

Hi Vedant,

  Clang + llvm build together takes around 26 GB. I was curious if there
are any tricks to reduce the size as I need only C and C++ languages in
frontend and RISCV ISA for llvm backend. For the backend I am already
passing -DLLVM_TARGETS_TO_BUILD=RISCV to generate the backend only for
RISCV. I was curious if I can do something to reduce the size of the clang
build.

Some ideas:

1. It sounds like you might have configured a Debug build. If you don't
need to debug clang, you can configure a Release build instead, which will
be much smaller.

2. You can enable a shared-library build to cut down on binary size
(-DBUILD_SHARED_LIBS=On). That means there will be just one copy of the
code from each llvm library in your build directory, instead of two.

3. You can pare down the list of targets you build. Just building the
"clang" target is much faster, and takes much less space, than building the
full suite of llvm tools (which is what you get if you run "ninja" or
"make").

4. If you're building a Release clang, you can use -Os or -Oz, instead of
-O3 which is the default.

I'd add on top of this to enable only the backend you care about, for
example `-DLLVM_TARGETS_TO_BUILD=X86`, disable plugins
`-CLANG_PLUGIN_SUPPORT=OFF` and try to enable LTO `-DLLVM_ENABLE_LTO=ON`
(the latter may not be a win though, but with -Os hopefully it will!).

The clang binary can get down to ~50MB, but I suspect getting it smaller is
likely gonna require more intrusive work.

Best,

2017-09-22 11:04 GMT-07:00 Vedant Kumar via cfe-dev <cfe-dev@lists.llvm.org>:

Hi Vedant,

Clang + llvm build together takes around 26 GB. I was curious if there are any tricks to reduce the size as I need only C and C++ languages in frontend and RISCV ISA for llvm backend. For the backend I am already passing -DLLVM_TARGETS_TO_BUILD=RISCV to generate the backend only for RISCV. I was curious if I can do something to reduce the size of the clang build.

Some ideas:

  1. It sounds like you might have configured a Debug build. If you don’t need to debug clang, you can configure a Release build instead, which will be much smaller.

  2. You can enable a shared-library build to cut down on binary size (-DBUILD_SHARED_LIBS=On). That means there will be just one copy of the code from each llvm library in your build directory, instead of two.

  3. You can pare down the list of targets you build. Just building the “clang” target is much faster, and takes much less space, than building the full suite of llvm tools (which is what you get if you run “ninja” or “make”).

  4. If you’re building a Release clang, you can use -Os or -Oz, instead of -O3 which is the default.

I’d add on top of this to enable only the backend you care about, for example -DLLVM_TARGETS_TO_BUILD=X86, disable plugins -CLANG_PLUGIN_SUPPORT=OFF and try to enable LTO -DLLVM_ENABLE_LTO=ON (the latter may not be a win though, but with -Os hopefully it will!).

The clang binary can get down to ~50MB, but I suspect getting it smaller is likely gonna require more intrusive work.

Yeah. I also forgot about -DCLANG_ENABLE_ARCMT=Off and -DCLANG_ENABLE_STATIC_ANALYZER=Off, which can cut down on build-time a bit.

vedant

Hi,

Are you sure you ar not mixing things? Clang is the frontend for C family languages (and name of compiler using it) . There are LLVM based compilers for other languages but those are different projects and not part of clang or llvm repositories. According to clang contains small amount of code for invoking GCC based fortran and java compilers and clangFormat but I doubt those add a significant size increase compared to the rest of clang.

Best regards,

Kārlis Seņko