Author Topic: LLVM Clang precompiled headers Code::Blocks 20.03  (Read 997 times)

Offline camilocc

  • Multiple posting newcomer
  • *
  • Posts: 23
    • ca2 Software Development
LLVM Clang precompiled headers Code::Blocks 20.03
« on: April 13, 2020, 01:36:22 pm »
Hi Dear Code::Blocks Team,

I had just built Code::Blocks 20.03 for CentOS 8.1 .

I realized I cannot build large project in fair amount of time because cannot use precompiled headers with GCC at EL8 (CentOS 8 ). The EL8 hardened build requirement prevent GCC from using precompiled headers due to its precompiled header strategy - it relies on fixed addresses which is incompatible with PIE (Position independent Executable) build.

So I am giving a try on using LLVM Clang compiler on Linux, initially just at CentOS 8.

I am trying to build a simple project to test precompiled headers with LLVM Clang Compiler.

I selected the precompiled header strategy of putting the precompiled header next to the original header.

The precompiled header is never generated. I tried other precompiled headers strategy and it is not generated either.

As workaround, tried to use custom build for the framework.h header file with -emit-pch Clang option.

I selected Build File on File properties and set build priority to 0.

It still doesn't build the header.

I could put -include-pch framework.h.pch at Compiler Options for the entire project.

It throws error that precompiled header doesn't exist.

Am I doing something wrong?

Does Code::Blocks implement precompiled headers for LLVM Clang compiler?

Best regards,

Camilo
 

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7018
    • My Best Post
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #1 on: April 13, 2020, 02:27:34 pm »
Did you set the file property of the PCH file to be compiled?

PCH means Precompiled header

Edit: I have never used LLVM/CLang to compile a header; it might not work.
Edit2: I am downloading .5 gigs to see if it works on Windows under MSys2 64 bit

Tim S.
« Last Edit: April 13, 2020, 02:34:25 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12911
    • Travis build status
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #2 on: April 13, 2020, 07:45:23 pm »
The EL8 hardened build requirement prevent GCC from using precompiled headers due to its precompiled header strategy - it relies on fixed addresses which is incompatible with PIE (Position independent Executable) build.
What do you mean? You can pass an option to disable PIE with a compiler/linker option. Wouldn't this work? If GCC is broken why would clang work?
I'm not sure if anyone has tested PCH for clang, I suppose they are broken, you could probably inspect the various compiler related xml files to see if there is something missing in the ones for llvm.

And clang is not really efficient with pch - https://randomascii.wordpress.com/2020/03/30/big-project-build-times-chromium/ :) Next thing to try is the unity-build craze. :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7018
    • My Best Post
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #3 on: April 13, 2020, 08:37:59 pm »
https://clang.llvm.org/docs/PCHInternals.html#using-precompiled-headers-with-clang

I think you will find that C::B does not support PCH using the clang compiler.

But, if you put the PCH into a separate target you should be able to add the pch option to it.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline camilocc

  • Multiple posting newcomer
  • *
  • Posts: 23
    • ca2 Software Development
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #4 on: April 14, 2020, 02:33:49 am »
Hi obfuscated,

I think clang doesn't have problems with EL8 (PIE compilation). GCC certainly cannot make currently make their precompiled header work at EL8, because of hardened builds with PIE. The header precompiling in GCC uses functions fixed addresses which implementation gets broken with PIE builds.

https://bugzilla.redhat.com/show_bug.cgi?id=1721553

Best regards,

Camilo

Offline camilocc

  • Multiple posting newcomer
  • *
  • Posts: 23
    • ca2 Software Development
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #5 on: April 14, 2020, 02:37:46 am »
Hi Tim,

Where can I ask to add for this support for Clang precompiled headers at Code::Blocks?

I tried to use "Custom Build" for the framework.h with -emit-pch flag but it didn't work. (I had chosen Compile File and set priority to 0).

It didn't build the .pch file.

Maybe it be because of this CodeBlocks I got at CentOS was built with WGTK3?

Best regards,

Camilo

Offline camilocc

  • Multiple posting newcomer
  • *
  • Posts: 23
    • ca2 Software Development
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #6 on: April 14, 2020, 02:44:05 am »
Hi stahta01,

Yes, I set the framework.h (the file to be precompiled), to be compiled (like headers are not normally selected to be built, by selecting "Compile File" at Framework.h file Properties Dialog). (But not linked).
I also tried to set the Custom Build (at Advanced Tab for framework.h file Properties Dialog).
I tried some combinations of the suggested options at the bottom of Custom Build (written in Red) plus -emit-pch flag.
Also set the priority slider all way to left (0).
When I select Code::Blocks to do the build, only the .cpp is compiled.
Even after Clean Workspace.
The framework.h build step is not run.

Best regards,
Camilo

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12911
    • Travis build status
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #7 on: April 14, 2020, 09:05:10 am »
I guess the steps are like these:
1. select the header, mark it for compilation, edit its command (right click -> properties -> advanced) -> add the emit-pch option
2. add the include-pch option to compiler options (there are chances this would break the compilation of the pch)
3. build

2. step to probably could be replaced by just including the pch in the source.

p.s. even if you report it as a ticket I have zero intention to work on this. I might apply a patch, but I think pch and unity builds are a waste of time and a gimmick.
p.p.s. cb uses pch and builds fine with clang, I'm not sure why this isn't "just works" (tm) style of thing.
p.p.p.s. pch in cb is wasting me so much time when I'm iterating on something...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline camilocc

  • Multiple posting newcomer
  • *
  • Posts: 23
    • ca2 Software Development
Re: LLVM Clang precompiled headers Code::Blocks 20.03
« Reply #8 on: April 20, 2020, 03:02:27 am »
I could make it work at Ubuntu 19.10 with Code::Blocks 20.03 from the ppa:codeblocks-dev/release

For C++ project.

for framework.h to produce precompiled header

1. Select at Main Project Options the Precompiled Header Strategy : Generate PCH alongside original header (default) (don't know if this flag actually currently makes any difference for this).
2. Create framework.cpp that includes just framework.h.
3. Select at framework.cpp properties for it just to be built (not linked) and lower priority to 0 to make it build firstly.
4. At framework.cpp properties > advanced tab, check custom build and set it to:

$compiler $options $includes -x c++-header framework.h -o framework.h.gch

5. For the project, set the extra compiler option:

-include "framework.h"

If this option is not set, clang++ won't use the precompiled header even if its present at the folder or even if it is included inside the cpp explicitly.

I couldn't set these properties I had set for framework.cpp at framework.h. Code::Blocks seems not to build it when using LLVM CLang compiler (it shows not dimmed at the project tree, but it still doesn't built.)
And the -emit-pch seems to be a clang option for another environment or situation, not for this C++ thing.

I am facing another problem now. The C11 flag (for clang) and C++17 (for clang++) keep offending each other on LLVM Clang projects with blend of C++ and C files. This used to happen with GCC compiler but it was fixed. Now it seems to be a problem with LLVM Clang compiler (at Ubuntu Linux, at least). (I think it may happen because the -include "framework.h" is there (in the C files (clang)) and the precompiled-header was compiled with C++17 options for C++.)

Best regards,

Camilo