Gregory Shimansky
Gregory Shimansky

Reputation: 103

How to conditionally specify C compiler defines in bazel?

I am trying to convert Makefile build into bazel, and need to reproduce the following condition to specify CPU capabilities defines for C code compilation:

HAVE_AVX2 := $(shell grep avx2 /proc/cpuinfo)
ifdef HAVE_AVX2
$(info Checking for AVX support... AVX and AVX2)
CFLAGS += -DRTE_MACHINE_CPUFLAG_AVX -DRTE_MACHINE_CPUFLAG_AVX2
else
HAVE_AVX := $(shell grep avx /proc/cpuinfo)
ifdef HAVE_AVX
$(info Checking for AVX support... AVX)
CFLAGS += -DRTE_MACHINE_CPUFLAG_AVX
else
$(info Checking for AVX support... no)
endif
endif

Is it possible to implement such conditional in bazel? From what I have found, cc_library has defines and copts where I could use a select function, but I cannot understand what kind of condition I can use inside of select.

Upvotes: 3

Views: 3161

Answers (1)

Dmitry Lomov
Dmitry Lomov

Reputation: 1406

Take a look at https://docs.bazel.build/versions/master/be/general.html#config_setting.

Generally, you do something like

config_setting(
    name = "avx2",
    values = {
        "define": "avx2=yes"
    }
)

and the you can select on :avx2 condition:

cc_library(...
   copts = select({":avx2":[...], ...})

and run bazel with

bazel build --define avx2=yes ...

Upvotes: 5

Related Questions