Limiting the Scope of Variables in CMake to specific Projects/Subdirectories

I would like to set a variable that is available for modification in a third-party library which I have included as a subdirectory (add_subdirectory(.../third_party/libA)). The library allows me to enable/disable certain portions of the build process such as to including testing targets, for example: set(BUILD_TESTS OFF).

However, what am I to do if multiple libraries contain the same option, named the same? I understand there exists the PARENT_SCOPE option of set() for the “opposite direction” of what I am looking to accomplish, but is there a way to restrict the scope somehow such that I can do the following (simulated behavior below):

project(my_project)

set(libA_BUILD_TESTS OFF) # I DO NOT want to build tests for libA
add_subdirectory("${my_project_SOURCE_DIR}/third_party/libA")

set(libB_BUILD_TESTS ON) # I DO want to build tests for libB
add_subdirectory("${my_project_SOURCE_DIR}/third_party/libB")

Because I have no control over the variable names of the third-party library, variables libA_BUILD_TESTS and libB_BUILD_TESTS do not actually exist. They both simply have BUILD_TESTS.

  1. Would it be possible to do this and expect each should be set properly? I am not familiar with
    how CMake handles the timing of variable resolution…

    set(BUILD_TESTS OFF)
    add_subdirectory(.../third_party/libA)
    
    set(BUILD_TESTS ON)
    add_subdirectory(.../third_party/libB)
    
  2. Is there a more explicit/safe way to declare that I want those two to be different
    beyond sharing the same variable name? To me, reusing the same variable name seems
    fundamentally prone to error and particularly “gross.” I do not like this because it
    makes the variable “stateful” which makes things harder to track during the build.

  3. Am I even thinking about this fundamental variable-setting process correctly? Perhaps
    my feelings in #2 are simply not “the way things are done.”

Some random things I tried (unsuccessfully):

Settings properties specific to the subdirectory:

set(libA_dir "${my_project_SOURCE_DIR}/third_party/libA")
set(libB_dir "${my_project_SOURCE_DIR}/third_party/libB")

set_property(DIRECTORY ${libA_dir} PROPERTY BUILD_TESTS OFF)
set_property(DIRECTORY ${libB_dir} PROPERTY BUILD_TESTS ON)

Settings target properties (doesn’t work as target doesn’t yet exist):

# does not work because the libA target does not yet exist
set_target_properties(libA PROPERTIES BUILD_TESTS OFF) 

add_subdirectory("${my_project_SOURCE_DIR}/third_party/libA")

# this does work based on how the third party CMake was written...
find_package(libA REQUIRED) 

Setting target properties after target is defined (doesn’t work because the logic has
already been processed before seeing my set_target_properties):

add_subdirectory("${my_project_SOURCE_DIR}/third_party/libA")

# does not work because the subdir was already included and seems to have not respected
# the options here due to timing/ordering.
set_target_properties(libA PROPERTIES BUILD_TESTS OFF)

# this does work based on how the third party CMake was written...
find_package(libA REQUIRED) 

Ultimately: I am looking to understand methods for ensuring “isolation” of variable assignment between these two libraries, especially when the same variable would have two different values (i.e. “I want to build tests for libA but not for libB”). Thank you.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật