'CMake building a project for two different targets with different build-tools

I have a project that I compile to two very different target (desktop and web-assembly), using a different environment, different compiler and different parameters.

So far I have two CMakeLists files, building into two different folders. But that forces me to copy-paste the list of source files, some configuration about the version etc. And they cannot be in the same folder because of the fixed name for CMakeLists files.

I would like to have a common cmake file that contains all the shared project configuration, while configuring the build differently depending on an option for example, and including one of the two sub-cmake files that would be specific to each build.

What is the modern-cmake strategy to do this?
I see answers about debug/release builds or shared/static but I fail to understand one best practice of how to define different targets+buildchains depending on an option.

Should I use include and an option?
Is passing the shared configuration and source files as variables a good practice?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source