Bug 9866

Summary: BASE_DIR usage
Product: buildroot Reporter: Jean-pierre Cartal <jpcartal>
Component: OtherAssignee: unassigned
Status: RESOLVED INVALID    
Severity: enhancement CC: buildroot, yann.morin.1998
Priority: P5    
Version: 2017.02   
Target Milestone: ---   
Hardware: All   
OS: Linux   
Host: Target:
Build:

Description Jean-pierre Cartal 2017-05-16 13:31:57 UTC
I'm trying to use the printvars target from a post image shell script and I would like this script to support both default output directory ie buildroot/output as well as O= option.
Thus I'm calling the printvars target as follow :
make -C ${BASE_DIR} printvars
However this code will only work if O= option is used, since in this case the BASE_DIR directory will contain the main Makefile.
Is there any variable that will contain the directory which contains the main Makefile file ?
Wouldn't it be more coherent if the BASE_DIR would point to directories with similar content without depending on O= option ?
Comment 1 Thomas Petazzoni 2017-05-16 16:03:20 UTC
According to the Buildroot manual:

"Again just like for the post-build scripts, the scripts have access to the environment variables BR2_CONFIG, HOST_DIR, STAGING_DIR, TARGET_DIR, BUILD_DIR, BINARIES_DIR and BASE_DIR."

So why bother re-running make printvars to get BASE_DIR, if it's already available in the environment?
Comment 2 Jean-pierre Cartal 2017-05-16 17:36:38 UTC
Sorry, my explanation was not clear enough.
Let's try again ;-)

I have several build environments, some are using out-of-tree builds using the -O option, others are using the default output location.

Now let's say I want to get the value of BR2_ARCH variable in the post image shell script using the printvars target, I need to find the location of the Makefile I should use.

My idea was to use the already available BASE_DIR variable as follow :
make -C ${BASE_DIR} printvars 

This is working fine for out-of-tree environments, since the ${BASE_DIR}/Makefile file will point to the makefile wrapper.
However for default build environment, ${BASE_DIR} will point to the output directory where there is no Makefile available.

For the time being I added a test in my script as follow :
#Out-of-tree build case
if [ ${BASE_DIR}/Makefile ] ; then 
make -C ${BASE_DIR} printvars
# default build environment case.
else
make -C ${BASE_DIR}/../ printvars
fi;

So I had several questions :
1) Am I using the correct method to get the current build Makefile location ?
2) Wouldn't it be more consistent for ${BASE_DIR} to always point to a location with the same content/structure for default and out-of-tree builds or maybe to have another variable that would give access to current build Makefile location ?

Thanks for your help.
Comment 3 Yann E. MORIN 2017-05-16 17:42:45 UTC
Try with something like:

    make -C "$(dirname "${BR2_CONFIG}")" printvars

Ideally, we would export CNFIG_DIR (which is exactly the directory
you get above), but CONFIG_DIR clashes with various buildsystems,
like (IIRC) U-Boot...
 
Regards,
Yann E. MORIN.
Comment 4 Peter Korsgaard 2017-05-16 19:02:38 UTC
(In reply to Yann E. MORIN from comment #3)

Actually you don't need to do anything like that. All scripts are executed from the buildroot toplevel directory, so the only thing you need to do is to ensure the correct .config is used (.config or $(O)/.config). This is taken care by the BR2_CONFIG variable, so just do:

make BR2_CONFIG=$BR2_CONFIG printvars
Comment 5 Yann E. MORIN 2017-05-16 19:11:31 UTC
(In reply to Peter Korsgaard from comment #4)

Except that in this case you'll be missing on any br2-external tree, or any local.mk, as:

  - br2-external is set in .br-external in CONFIG_DIR
  - local.mk is by default in CONFIG_DIR

but CONFIG_DIR is not in .config...

In retrospect, I think we should just export CONFIG_DIR, as it's
CONFIG that can clash, not CONFIG_DIR (AFAICS...)
Comment 6 Peter Korsgaard 2017-05-16 19:42:31 UTC
(In reply to Yann E. MORIN from comment #5)

Ahh yes, as you can see I don't use BR2_EXTERNAL (and local.mk normally doesn't matter for a post-build / post-image script).

Grepping the linux and u-boot sources, I do see some references to CONFIG_DIR though, so we have to be careful.
Comment 7 Yann E. MORIN 2017-05-16 20:24:25 UTC
Jean-Pierre,

I'm marking this patch as invalid, because this is not really a bug.

Thanks for the report.