cmake

How to get target full output name from CMake file API?


I'm writing some scripts to collect build outputs from CMake-managed project. It seems all significant information about targets are exposed via CMake file API which is a JSON file reporting detailed project information.

I browsed some of the files. The JSON contains entry nameOnDisk, but it only contains the file base name without directory. Also there are entry paths-build, but it is actually the CMAKE_BINARY_DIR where you define the target, and the actual location of the target output file may be still different from that.

So how could I get the reliable full path to the target output file?


Solution

  • To get the full path of a target output in a CMake managed project using the File API, the most reliable way is to check the "artifacts" field in the target.json. This usually includes the relative or absolute path to the built output like executables or libraries. If it's a relative path, you can safely prepend it with the paths.build value. Avoid relying only on nameOnDisk, as it gives just the base file name. This approach has worked well in my scripts for collecting target outputs.