Excluding from build specific directories from library

I’m using a header-only library that includes a src directory with benchmarks and tests. I want the build process to ignore everything in the library’s src directory.

I can add a library.json file to the root directory of the library wherein I specify a source filter. Works great, but of course it’s manual and would have to be done by any project developer, because I don’t want to fork the lib maintain my own repo just to add the file. (And the library author is unlikely to accept a PR with PlatformIO specific file.)

I tried specifying a source filter in platformio.ini like -<../.piolibdeps/library_name/src> (and variants with ...src/*.cpp, ...src/*/*.cpp, ...src/subdir/*.cpp), but that didn’t work.

EDIT 20181123: src_filter works perfectly for this purpose. My test was bad. Also, Ivan’s alternative below works, too.

Should I be able to exclude library source files with a src_filter?

How about to lib_ignore = MyLIb whole library and add relative include path to build_flags = -I .piolibdeps/library_name?

That works. Thanks!

Also, src_filter works, too. I was putting the src_filter line in the wrong env, so needless to say it was having no effect.

1 Like

Sorry to dig up this old thread, but my question seems to be closely related.

I got a project that primarily is meant to develop a library. The library code is under <project>/lib/<lib name>/src/... and is a local of a Github repository.

In <project>/src/..., the test sources using the library are found. platformio.ini has several additional [env] entries for different test source constellations.

Now in <project>/lib/<lib name>/src/Linux/... some additional files for the Linux environment are held, that shall not be compiled when building the test sources.

How can I achieve in PlatformIO to have these files excluded from building?

If the library has source files which are only supposed to be compiled under some platform or operating systems, the documentation has an example (“HAL based library”) with an extra_script that lets you use a src_filter expression to exclude certain files from the build, if you detect in Python code that they must not be included. That is the only way I know of that handles this in the library, and not via src_filter or extra scripts in the project.

Ough. That is not what I had hoped for - a simple exclude_src = ... or such.

I think I will rather try to move the Linux source files to another path normally not considered in the build.

Thanks anyway!

There is srcFilter but it’s unconditional exclusion / inclusion, which wouldn’t work for you if I understood you correctly.

Yes, you did. And src_filter seems to work correctly with files under the src directory only, whereas my files reside in the lib tree.

I managed to have the Linux files moved to an examples folder for now and successfully was able to build again.

Thanks for the links. I’ve followed the suggestions here,
in platformio.ini I created a


extra_scripts = pre:build_files_exclude.py
custom_build_files_exclude = *\file1.cpp *\file2.cpp

and then file build_files_exclude.py

#advanced-solutions:debugging build_files_exclude.py
# run before the build process.
# This excludes files in libs from the build process
# https://docs.platformio.org/en/latest/scripting/examples/platformio_ini_custom_options.html
# Use:
# custom_build_files_exclude

# Concepts in https://github.com/platformio/platformio-core/blob/develop/platformio/project/config.py
# could be in
# https://docs.platformio.org/en/latest/projectconf/sections/platformio/options/directory/shared_dir.html#projectconf-pio-shared-dir
# shared_dir



# Get custom_ list of files to skip

custom_build_files_exclude = env.GetProjectOption("custom_build_files_exclude")
print(" ** Custom_ skip build targets** ", custom_build_files_exclude )

def skip_tgt_from_build(env, node):
# to ignore file from a build process, just return None
return None

# iterate over all files
temp = custom_build_files_exclude.split(" ")
for value in temp:
     env.AddBuildMiddleware(skip_tgt_from_build, value):