0

Basically, I need to amalgamate the source code into a single file . gcc -E almost does the job for me, except for the fact that I don't need to print the codes from system headers. That is it should print my code as well as any codes from 3rd party library used but nothing from a system library.

I was wondering if I can tweak gcc -E such that it suppresses printing the codes from system headers. In case that's already achievable by a combination of some other options, you can mention that as well.

I have gone through this answer. this hack doesn't work because the 3rd party libraries themselves call system headers which need to be suppressed as well.

providerZ
  • 315
  • 11
ishandutta2007
  • 16,676
  • 16
  • 93
  • 129
  • 2
    As developers could imagine that thoe options are really needed only for compiler, toolchain and project tree debugging, there is no really easy or direct options, which leave the result as a functional code and without system headers (problems often come from misconfigured, or header name conflict). What kind of xy problem such solution must solve? – Swift - Friday Pie Dec 18 '22 at 08:42
  • @Swift-FridayPie okay so whats the hard option you can think of, something like forking the gcc preprosessor and modifying it ? – ishandutta2007 Dec 18 '22 at 08:59
  • @Swift-FridayPie what do you mean by `xy problem` ? I just need to amalgamate my source into a single file without any 3rd party dependency – ishandutta2007 Dec 18 '22 at 09:03
  • 1
    @ishandutta2007 [What is the XY problem?](https://meta.stackexchange.com/q/66377/1088098) – justANewb stands with Ukraine Dec 18 '22 at 09:04
  • Actual problem is code amalgamation. Why? because I need my code in a single file and without any 3rd party dependency. – ishandutta2007 Dec 18 '22 at 09:05
  • 1
    but why do you need it in a single file? – Neil Butterworth Dec 18 '22 at 09:09
  • @NeilButterworth thats how my environment is. There I have many constrains to work. with. – ishandutta2007 Dec 18 '22 at 09:11
  • @Swift-FridayPie I have tried [this](https://github.com/Viatorus/quom) amalgamator but it has many limitations, it's no where close to `gcc -E` but `gcc -E` has the problem of expanding system libraries as well which I dont want. if you go through this [ticket](https://github.com/Viatorus/quom/issues/52) you will understand in detail – ishandutta2007 Dec 18 '22 at 09:12
  • 1
    better change your environment - you may be able to build an amalgamation of your code, but it will still be dependent on anly 3rd party code you call – Neil Butterworth Dec 18 '22 at 09:13
  • @NeilButterworth LOL. thats not in my hand. Thats what the entire challenge is all about. – ishandutta2007 Dec 18 '22 at 09:14
  • @NeilButterworth `gcc -E` expands in all the 3rd party refs very smoothly. no issues there. issue is it also expands the system libs which I dont want. – ishandutta2007 Dec 18 '22 at 09:16
  • The discussions in this comments thread are not even at https://stackoverflow.com/a/62578619/865220 level yet. Guys First you have to be at that page to suggest me further enhancements. – ishandutta2007 Dec 18 '22 at 09:20
  • the gcc solution wouldn't ONLY amalgamate code, it often would make it unreadable by unfolding all macro definitions and compiler-specific macro names (which might be built-in into compiler's code or taken from some built-in headers which weren't mentioned). I'd pretty much started to write a script which would unfold headers for me and remove duplicates (relatively easy) if I actually needed because that's a relatively unique task. – Swift - Friday Pie Dec 18 '22 at 09:30
  • @Swift-FridayPie Readbility is not an issue for me since my code is small. It adds 2 comments(starting and ending) in the generated code to indicate where from the block between those 2 comments is fetched. and that is nested as well. do you mean to parse the generated code based on those comments structure into a parse-tree? – ishandutta2007 Dec 18 '22 at 09:40
  • @Swift-FridayPie But thats going to be too much error prone. my point what if we could tweak the pre-processor itself to suppress those lines while printing based on where from its being fetched? – ishandutta2007 Dec 18 '22 at 09:43
  • @Swift-FridayPie and then on my modied gcc preprocessor I can run something lilke `gcc -E -my_new_option` – ishandutta2007 Dec 18 '22 at 09:45
  • have you looked at how sqlite manages its amalgamation? – Neil Butterworth Dec 18 '22 at 09:47
  • @NeilButterworth I guesss [this](https://github.com/sqlite/sqlite/blob/master/tool/mksqlite3c.tcl) one Something wriiten in tcl. Mind you I dont need to amagamate the entire 3rd party libs . Since I only need to extract out few function out of the 3rd party libraries which my code calls. the final amagamated code will be small. – ishandutta2007 Dec 18 '22 at 10:20
  • @NeilButterworth for example say my code is 100 lines long and it uses two 3rd party libs of 1M+ lines each now , the final amagamated code might be 100lines of mine plus few hundred lines from each of the 3rd party libs. so in total it may be less than 1K Lines which is no where close to 2M lines of the two libs. Hope you got how my scenario is not like sqlite where they are flattening the entire library. – ishandutta2007 Dec 18 '22 at 10:27
  • 1
    I was going to suggest using something like [pcpp](https://github.com/ned14/pcpp), but it sounds like you might need something more specialized if you also want to glue on and reduce the contents of 3rd party libraries to what you use (unless these have very heavy use of `#ifdef` in them to filter on). – Hasturkun Dec 18 '22 at 10:35
  • I've never used `pcpp`. It looks like it has options for passing through `#include` directives it doesn't in its search paths (which you can just not pass in) without fiddling in its internals or API. – Hasturkun Dec 18 '22 at 13:51
  • sorry I correct myself, preprocessor pulls in everything but amagamator like quom doesn't. – ishandutta2007 Dec 18 '22 at 15:10
  • `Mind you I dont need to amagamate the entire 3rd party libs . Since I only need to extract out few function out of the 3rd party libraries which my code calls.` this is non-trivial task provided that libraries today are interconnected entities, not just "collection of functions" – Swift - Friday Pie Dec 18 '22 at 18:38
  • @Swift-FridayPie Have looked at open source tool like quom? No wonder it is non-trivial , which is why tools like quom are missing many scenarios? If you believe the way they have coded is the best was to go I may have to enhance quom to add support for more libraries that I work with.Do take a look and express your views on the best approach to go about it, – ishandutta2007 Dec 18 '22 at 19:41
  • Quom simply stitches files based on include. Not sure about missing scenarios, it seems to treat `#pragma once` (which is not standard C++) just as line of code, which may result in incorrect amalgamation. – Swift - Friday Pie Dec 18 '22 at 23:16
  • @Swift-FridayPie So what are the tools you suggest for someone who needs to do it frequently? Doing manually takes hours even if you need to extract small logic from a large package . In python what I am doing is while running code without any amalgation I make a note of all file-names referred first. Then copy paste entire file content on top of my program for all those files. and then run my code with code coverage tool enabled. It gives me idea to what lines didint get referenced and can be deleted. I am not sure if I can do that in C++ that well. – ishandutta2007 Dec 19 '22 at 03:14

0 Answers0