Target dependant properties of a source file used by multiple targets

Consider a project that defines multiple object libraries from the same source file:

add_library (A OBJECT
  foo.cpp
  bar.cpp
)

add_library (B OBJECT
  foo.cpp
  bar.cpp
)

My goal is to set a preprocessor definition (say FOO) specifically for foo.cpp to a value dependent on the target that generates the corresponding object file. In particular, I want to avoid a target-wide visibility of the definition, e.g., by using target_compile_definitions.

Is there a way to limit the scope of set_source_files_properties to specific targets? I feel that a generator expression would be the appropriate tool here, something along the lines

set_source_files_properties (foo.cpp PROPERTIES COMPILE_DEFINITIONS
  $<$<TARGET_SOURCE:A>:FOO=A>
  $<$<TARGET_SOURCE:B>:FOO=B>
)

However, I’m not able to identify an existing (target-dependent) generator expression that is suitable for this purpose.

The proper way to do this is to define different targets for the different combinations of macro definitions and link against those different targets to pick up the different versions of the files.

Just because it is an object library doesn’t mean the file is recompiled for each target that links against the object library (that would, in fact, defeat the purpose of object libraries).