0
.
|-bin
| |-Debug
|-source
|-sh
| |-runme.sh
|-CMakeLists.txt

Normally, I cd bin/Debug. Then cmake -DCMAKE_BUILD_TYPE=Debug ../../ following cmake --build .

Now, I want to create a bash script sh/runme.sh that I can call from main . directory using sh sh/runme.sh Debug, which is intended to do the same work as described in the previous line.

My present runme.sh:

cd bin/"$1"  # of course this does not work.
cmake -DCMAKE_BUILD_TYPE=Debug ../../
cmake --build .

If I run cmake -DCMAKE_BUILD_TYPE=Debug . following cmake --build . from main . directory, it creates CMakeFiles/, cache, makefile etc. relative to main . directory and not in the respective bin/Debug directory.

What and where (CMakeLists.txt or sh/runme.sh) should I change to make this work? Preferably to CMakeLists.txt. I want to keep main directory clean.

Dr.PB
  • 959
  • 1
  • 13
  • 34
  • `# of course this does not work.` why?, do you get an error message? What does it say? – KamilCuk Sep 30 '21 at 09:48
  • calling `cd` from bash script does not actually serve the intended purpose. [similar](https://stackoverflow.com/questions/255414/why-cant-i-change-directories-using-cd-in-a-script) – Dr.PB Sep 30 '21 at 11:17

1 Answers1

0

First cd into your root repository. You could get the path to current script and be relative to it, or if you're for example using git, get path to root dir of git repo.

Also, you do not need to cd (except for ctest). Specify the directory to cmake.

#!/bin/bash
cd "${BASH_SOURCE[0]%/*}"/..
if (($# != 1)); then : handle errror; fi
b=bin/"$1"
cmake -S . -B "$b" -DCMAKE_BUILD_TYPE="$1"
cmake --build "$b"

I like doing Makefiles with all the shortcuts, because of the interactive completion of make in shell.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks for the `-B`. How to call `make clean` from root repo, in this case? – Dr.PB Sep 30 '21 at 11:14
  • `make clean from root repo, in this case?` In the case you write your own Makefile, _you_ write what `make clean` does, for me it usually does `rm -r _build`. – KamilCuk Sep 30 '21 at 11:19