Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip file extensions with cmake (or any other portable build tool)?

Tags:

build

cmake

I'm working on a project that consists of multiple small executables. The executables are meant to be run from the terminal (or command prompt), and may be written in any programming language. Those written in interpreted languages have a shebang line for unixy systems, while their file extension is added to the PATHEXT environment variable on Windows.

To make usage of the executables consistent across all programming languages and both major platform groups, I need to strip the file extension from the file name of interpreted programs on unixy systems. (By "consistent usage" I mean: just type the name of the program to launch it, without needing to specify its file extension.)

To go more into the concrete, suppose I write something like the following CMakeLists file:

project (Mixed Example)

add_executable (banana banana.cpp)
add_executable (grape grape.hs)
add_script? (orange orange.py)
add_script? (strawberry strawberry.lua)

install (TARGETS banana grape orange strawberry DESTINATION bin)

Then I want banana.cpp and grape.hs to be compiled in the usual way, while I want the file extensions of orange.py and strawberry.lua to be stripped conditionally, depending on the platform. Thus, the bin directory should contain the following files on a unixy system:

banana grape orange strawberry

and the following on Windows:

banana.exe grape.exe orange.py strawberry.lua

How do I do that?

like image 770
Julian Avatar asked Apr 25 '12 16:04

Julian


1 Answers

If you don't treat these script files as CMake targets, and instead treat them as files, you should be able to do:

project (Mixed Example)

add_executable (banana banana.cpp)
add_executable (grape grape.hs)

install (TARGETS banana grape DESTINATION bin)
if (UNIX)
  install (FILES orange.py DESTINATION bin RENAME orange)
  install (FILES strawberry.lua DESTINATION bin RENAME strawberry)
else (WIN32)
  install (FILES orange.py strawberry.lua DESTINATION bin)
endif ()


If you want to use a function rather than calling install (FILES ... multiple times, you can do:

function (install_files)
  if (UNIX)
    foreach (file ${ARGV})
      get_filename_component (name_without_extension ${file} NAME_WE)
      install (FILES ${file} DESTINATION bin RENAME ${name_without_extension})
    endforeach ()
  else (WIN32)
    install (FILES ${ARGV} DESTINATION bin)
  endif ()
endfunction ()

install (TARGETS banana grape DESTINATION bin)
install_files (orange.py strawberry.lua)
like image 102
Fraser Avatar answered Oct 16 '22 19:10

Fraser