Search code examples
cgccsedmakefileinclude-path

Getting the compiler default include path into a makefile


I am can run a shell command to grab the default include path from the compiler (and append the -I):

arm-none-eabi-gcc -xc -E  -Wp,-v  /dev/null 2>&1 | sed -En '/#include <...> search starts here:/,/End of search list./{//!p;};' | sed 's/^ /-I/'

I am trying to add that inside a Makefile:

PROJECT_INC = $(shell arm-none-eabi-gcc -xc -E  -Wp,-v /dev/null 2>&1 | sed -En "/#include <...> search starts here:/,/End of search list./{//!p;}" | sed "s/^ /-I/")

Then I get an error:

Makefile:104: *** unterminated call to function `shell': missing `)'.  Stop.

I tried to use backticks instead, that does not help. Tried using single-quotes or double-quotes for the sed commands. Most of the similar problems I can find on SO are related to expansion when there is a $ in the command, but it should not be a problem here.

If I cut down the command:

PROJECT_INC = $(shell arm-none-eabi-gcc -xc -E  -Wp,-v /dev/null 2>&1  )

Then it seems to be cutting down the output. The shell would return

ignoring duplicate directory "/Users/mjeanson/miniconda3/envs/project/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/8.3.1/include"
ignoring nonexistent directory "/Users/mjeanson/miniconda3/envs/project/bin/../arm-none-eabi/usr/local/include"
ignoring duplicate directory "/Users/mjeanson/miniconda3/envs/project/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/8.3.1/include-fixed"
ignoring duplicate directory "/Users/mjeanson/miniconda3/envs/project/bin/../lib/gcc/../../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/include"
ignoring nonexistent directory "/Users/mjeanson/miniconda3/envs/project/bin/../arm-none-eabi/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /Users/mjeanson/miniconda3/envs/project/bin/../lib/gcc/arm-none-eabi/8.3.1/include
 /Users/mjeanson/miniconda3/envs/project/bin/../lib/gcc/arm-none-eabi/8.3.1/include-fixed
 /Users/mjeanson/miniconda3/envs/project/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/include
End of search list.
# 1 "/dev/null"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/dev/null"

Running this inside the Makefile, it would discard from the first line starting with #include, but otherwise it works. Not sure if that's going to be still be a problem after the piping to the sed command works fine(?)

If that's relevant (and not already obvious in the code snippets above), my environment is using a cross compiler (arm-none-eabi-gcc) and in a conda environment and all running on a Mac.

I don't think it should matter, but the reason why I need to do this is so that I can use some static analysis tool. When compiling the code itself, I do not need to add the default include path.

Bonus: I tried combining the sed commands in one but could not figure out how to do that...


Solution

  • Found the problem... the '#' character in the sed command was causing the issue and cause make to consider everything after as a comment.

    For posterity, the fixed command is much easier since the include path start with a ' ':

    arm-none-eabi-gcc -xc -E  -Wp,-v  /dev/null 2>&1 | sed -En '/^ /s/^ /-I/p'