Conditional Compilation of source files based on environmental variable in soong build system

3,688 views
Skip to first unread message

shalini...@gmail.com

unread,
Aug 5, 2017, 3:40:02 PM8/5/17
to Android Building

Hi,

I have been working with Android O preview source code and have been studying its new build system, soong.

I’m facing issue regarding conditional compilation of source files inside different directories based on value of environment variable.

Let’s say Android O-source code has two directories A and B. Each of the directories contain top level Android.bp inside them. I would like soong to conditionally compile sources in directory A or B depending up on the value of an environment variable $ENV_VARIABLE.

ENV_VARIABLE is not specified (null) or contains a string value.

If ENV_VARIABLE is not  null, Android.bp file in A should be compiled, otherwise Android.bp file in B should be compiled.

Currently, both the Android.bp files in A and B are compiled with soong. So, we need soong to compile Android.bp file in A or B based on the value of $ENV_VARIABLE.

 

I tried the below approach :

1.       Added  My_struct struct { Srcs []string } to varaibleProperties in build/soong/variable.go

+   My_struct struct{

+                Srcs[]string

+                }
2.+ My_struct       *string `json:",omitempty to productVariables in build/soong/variable.go 
3.Passed ENV_VARIABLE to My_struct in build/make/core/soong_config.mk

+   echo '    "My_struct": "$(ENV_VARIABLE)",'

I would like to change Android.bp in directory A in the following way so that whenever My_struct has a valid string, soong should compile sources specified in the subdirs field.


+               product_variables: {

+                   my_struct: {

+                       subdirs =[“../B”],

+                   },

+               },

 }

 

But subdirs is not a property in .bp module. Can you please provide help on how to achieve this?

 

Thank You.

 

Colin Cross

unread,
Aug 5, 2017, 4:50:02 PM8/5/17
to android-...@googlegroups.com
One of the goals of soong is to reduce the number of different ways we
compile the same code, which makes it much harder to determine if a
change is going to break a build. We try to move as many of these
decisions to runtime as possible. For example, you can compile the
two subdirectories as separate shared libraries, and then choose which
one to install and load at runtime.

For cases where you absolutely must handle it at compile time, you can
see examples at
https://android.googlesource.com/platform/art.git/+/master/build/art.go
or https://android.googlesource.com/platform/external/llvm.git/+/master/soong/llvm.go.
You will need to define your own module type that extends cc_library
or cc_binary, add a property struct to it that contains your subdirs
property, then in a load hook read the environment variable and copy
the values out of your property struct into the Srcs property.
> --
> --
> You received this message because you are subscribed to the "Android
> Building" mailing list.
> To post to this group, send email to android-...@googlegroups.com
> To unsubscribe from this group, send email to
> android-buildi...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-building?hl=en
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Building" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to android-buildi...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

praveen...@gmail.com

unread,
Sep 4, 2018, 10:43:20 AM9/4/18
to Android Building
Hi,
   I have been working on Android O recently and I want to have a conditional compilation as per the below scenario:
   
1) I have an .mk file @ ~/device/xxyyzz/abc.mk  ---- Here abc.mk is the target for which I will build this sandbox via lunch command.
 
   The abc.mk has a boolean flag declared by me as MAC_Serialization := TRUE/FALSE
   
2) In the location @ ~/hardware/interfaces/bluetooth/1.0/default/bluetooth_address.cc I have a custom logic of mine to be inserted for a piece of code to be compiled or not complied based on the boolean flag "MAC_Serialization"
   For this task I tried to access the boolean flag "MAC_Serialization" in the android.mk file @ ~/hardware/interfaces/bluetooth/1.0/default/Android.mk and set the CFLAG with an expectation that the bluetooth-address.cc will respond to this CFLAG. I observe that the value of boolean flag "MAC_Serialization" has no control over this module.
   
My Question: How do I have a conditional compilation in the source code @ ~/hardware/interfaces/bluetooth/1.0/default/bluetooth_address.cc based on the boolean variable "MAC_Serialization" declared in the top level abc.mk located @ ~/device/xxyyzz/abc.mk?

Not sure If I'm going the right direction on this, if so what will be the way to do this?


Thanks and Regards,
Praveen Tubachi.

Sumit Kumar

unread,
Mar 5, 2019, 10:53:39 AM3/5/19
to Android Building
Hi praveen,colin,shalini,
    I am stuck at a similar condition as you guys while implementing conditional compilation in the .bp files , i am working on Android-pi flavour,
    following are the steps that i have tried
   -> implemented my own go module named multi_display.go
       
func globalFlags(ctx android.BaseContext) []string {
    var cflags []string

        fmt.Println("Inside 1 globalFlags")
   if ctx.AConfig().IsEnvTrue("USE_MULTI_DISPLAY") {

        cflags = append(cflags, "-DMULTI_DISPLAY_SUPPORT")
        fmt.Println("Inside globalFlags")
    }
    return cflags
}


   -> included the same in build/soong/Android.bp
   -> with the .go file residing in the same folder
  i am able to compile android for the target but the flag "MULTI_DISPLAY_SUPPORT" is not propagating to hardware/libhardware/

Please let me know what have i misssed adding, any help in this regard would be apreciated.
Reply all
Reply to author
Forward
0 new messages