Sample Header Ad - 728x90

Compiling CFLAGS in a Yocto Recipe File

1 vote
0 answers
3996 views
**Issue:** I have 2 macros I added in a c file and I have 2 machines. Using Yocto recipe I wish to build the code with specific macros for specific machines. Breaking down the issue in sub parts - **hello.c**
// Simple hello.c program
#include

void main(){
	printf("Hello World!\n");

	//Introducing Macro 1 -- abc

	#ifdef abc
	printf("Day 1\n");
	#endif

	//Introducing Macro 2 -- xyz

	#ifdef xyz
	printf("Day 2\n");
	#endif
}
**Expected & Observed Output:**
gcc hello.c -o hello
./hello
Hello World!

gcc hello.c -Dabc -o test1
./test1
Hello World!
Day 1 

gcc hello.c -Dxyz -o test2
./test2
Hello World!
Day 2
**Makefile Contents:**
obj-m := hello.o

SRC := ${shell pwd}

CFLAGS += -Dabc

all:
	${MAKE} -C M=${SRC} ${CFLAGS}

test_install:
	${MAKE} -C M=${SRC} ${CFLAGS} test_install

clean:
    rm -rf *.o
Have checked the above Makefile contents using cat -e -t Makefile. It is properly terminated with $ at end of each line. Since I am using MAKE, it does have the gcc compiler. In the Makefile I have hardcoded CFLAGS="-Dabc" , intention being if the code is compiled accordingly then I can add ifeq condition to the Makefile handle the other CFLAGS="-Dxyz" **Recipe File Contents:**
S = "${WORKDIR}/helloworld/hello"

COMPATIBLE_MACHINE = "oldmachine|newmachine"
EXTRA_OEMAKE_oldmachine += "CFLAGS=-Dabc"
EXTRA_OEMAKE_newmachine += "CFLAGS=-Dxyz"

do_compile(){
	${EXTRA_OEMAKE}_${MACHINE}
}
The default function of do_compile is to run oe_runmake, I assumed that it will be picking up this CFLAGS which I am trying to pass. I checked the contents of the log.do_compile under tmp/work/... folder and I do not see the CFLAGS being used. The above recipe I also re-wrote as
do_compile_append_oldmachine(){
     CFLAGS="-Dabc"
}

do_compile_append_newmachine(){
     CFLAGS="-Dxyz" #Also tried passing EXTRA_OEMAKE_MACHINE as above
}
This method also, unable to find the CFLAGS part when I check the log.do_compile file under tmp/work/.. directory. How do I make use the CFLAGS so that it compiles the code as per the specific architecture? **Misc Details:** Under the specific meta layer, I have the directory like this. helloworld - hello.c - hello.o - Makefile Note: Due to less rep, could not add CFLAGS as a tag for this.
Asked by Nathan (31 rep)
Mar 27, 2023, 03:28 AM
Last activity: Jun 20, 2024, 06:28 AM