Makefile: copy perl/python files from source directory into build directory - c++

I have the following source directory structure
src:
dir1: c++ files, Makefile
dir2: perl/python scripts, Makefile
build:
bin:
binary-executables
bin-subdir: I want my perl/python files to be copied during the build process.
Also, When I do a make install, will the bin-subdir be copied into install/bin by default or I have to specify that as well?

Basically, when you run make X, you are telling Make to find target X in your Makefile. So if you have no install: target, nothing will happen. All of this really depends what is in your Makefiles. If you want to copy your perl/python files into the build directory, one way to do so is to write a Makefile target that runs a *sh command like mv dir2/*.pyc build; mv dir2/*.pl build, and require that target somewhere else in your Makefile. If you need a good Makefile tutorial, here's one that I started with.

Related

Compile a single file under CMake project?

I'm developing a C++ project which is going to be enclosed on a bigger one.
I've seen that on the bigger project (is a Qt application and it's being generated from qmake) I am able to compile a single file from the linux command line, just entering the relative path to the specific file as an argument to make.
On the other hand, I'm using CMake for my own project. When I modify some code for a compilation unit and I have to modify its header file, I have to wait a long time to compile its dependencies and then its own source file. But there are some situations in which I would prefer to check whether the source code in the *.cc file is compilable without errors.
Is there a way to generate a Makefile from CMake the way qmake does this? Switching to qmake is not an option anymore.
You do not have to add extra custom targets to your CMake scripts, as the Makefiles generated by CMake already contain .o targets for each .cc file. E.g. if you have a source file called mySourceFile.cc, there will be a Makefile in your build directory that defines a target called <Some Path>/mySourceFile.cc.o. If you cd into your build directory, you can use grep or ack-grep to locate the Makefile that defines this target, then cd into that Makefile's directory and build it.
E.g. suppose the command ack-grep mySourceFile.cc.o prints something like:
foo/bar/Makefile
119:x/y/z/mySourceFile.o: x/y/z/mySourceFile.cc.o
123:x/y/z/mySourceFile.cc.o:
124: # recipe for building target
Then you can build mySourceFile.cc.o by doing:
cd foo/bar && make x/y/z/mySourceFile.cc.o
CMake doesn't have a generic built-in way of doing this (it's an open issue), but if you're using the Ninja generator, you can can use a special Ninja syntax for building just the direct outputs of a given source file. For example, to compile just foo.o you would use:
ninja /path/to/foo.cpp^
Not out-of-the box. CMake does not expose those "internal" makefile rules in the main makefile.
You can do this only if you consider what kind of file structure CMake uses internally. You can e.g. for compiling a single .obj files using CMake generated makefiles call
make -f CMakeFiles/myProg.dir/build.make CMakeFiles/myProg.dir/main.cc.obj
when you have something like
cmake_minimum_required(VERSION 3.1)
project(myProg CXX)
file(WRITE "main.cc" "int main()\n{\nreturn 0;\n}")
add_executable(myProg main.cc)
To build src/foo.cpp alone:
cmake --build . --target src/foo.cpp.o
No, CMake does not offer built-in support to compile single files.
You have to add a target for each object file, maybe by a function iterating over all files of a directory.
Others have suggested ways to find the target name (ending in .cpp.o) from the .cpp filename, but if you already know the name of a target that will trigger compilation of the .cpp file and you're using ninja this suggestion should be easier.
First build the target:
ninja TriggersCppCompilationLib
Assuming your file was changed or was not yet built, ninja will print the full target name. When you see the name come up, hit enter so it is not overwritten. Then simply copy the name from the terminal (e.g. using tmux copy mode).

Build folder and makefile

The question comes from my puzzlement when compiling a makefile for Deep Learning framework Caffe on Ubuntu, but it relates, I believe, to a more general phenomenon of the nature of compiling a C++ makefile.
After "make all", the resulting files from the compilation were put in a hidden folder: .build_release, not in the respective folders where the cpp files are.
Then when I tried to run the following lines:
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
I was getting an error that the system does not find the file:
./create_mnist.sh: 16: ./create_mnist.sh: build/examples/mnist/convert_mnist_data.bin: not found
But the file actually existed in the .build_release folder.
What happened and how to fix this problem?
The issue is not with make, you simply need to follow the instructions carefully. The BUILD_DIR is specified by Makefile.config. By default this folder is named build. Once you followed the compilation instructions:
cp Makefile.config.example Makefile.config
# Adjust Makefile.config (for example, if using Anaconda Python)
make all
make test
make runtest
Navigate to build:
cd build
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh

Cmake generate independent makefiles

We are moving from MPC to CMake.
We provide a lib with some samples. The samples are coming with makefiles.
The problem is that the makefiles, generated by cmake contains absolute paths but not relative ones:
# The main all target
all: cmake_check_build_system
cd /.../Projects/cpp_trunk && $(CMAKE_COMMAND) -E cmake_progress_start /.../Projects/cpp_trunk/CMakeFiles /.../Projects/cpp_trunk/samples/CMakeFiles/progress.make
cd /.../Projects/cpp_trunk && $(MAKE) -f CMakeFiles/Makefile2 samples/all
$(CMAKE_COMMAND) -E cmake_progress_start /.../cpp_trunk/CMakeFiles 0
So, when it is copied it's become broken.
It there any way to work it around?
UPD: I have read the FAQ, but my question is still taking place, perhaps somebody managed to get around?
What I've done to get around this sort of thing is write a small wrapper Makefile around cmake. I put the Makefile at the project root, with contents like this:
all: cmake
cmake:
[ -f build/CMakeCache.txt ] && [ "$$(pwd)" != "$$(grep 'CMAKE_HOME_DIRECTORY:INTERNAL' build/CMakeCache.txt | cut -d '=' -f 2)" ] \
&& rm -rf build || true
mkdir -p build && cd build && cmake ..
make -C build
clean:
rm -rf build
There's probably a cleaner way to do it, but it works for me:
make # build in one directory
cd ..
olddir=$(basename $OLDPWD) && rsync -ravz $olddir ${olddir}-test && cd ${olddir}-test # copy to another directory
make # running make in the new dir triggers a full rebuild
make # running make a second time in the new dir does not rebuild
The makefiles created by CMake are not part of your source code base. The CMakeLists.txt files that you use as input to CMake are part of your source code base. When you copy your source code to a different place and want to build it there, build from your source code. That means re-running CMake. (And that's your workaround.)
I've been using CMake for over ten years continuously on one project. One of the handy tricks my team has learned is that you can have multiple copies of one part of your source code base on one development host that all share the same copy of the rest of your source code base. Try doing that with relative paths! We rely on the fact that every time we build source code in a new build directory, CMake will figure out the correct paths to all the source files, which are not necessarily the same relative to the new build directory as they were in the previous build.
The build files that are generated by cmake (makefiles, ninja files, etc), are going to have hardcoded paths and other not-portable stuff in them. That's ok. Treat them as temporary files that are part of the build process. You will only version the CMakeLists.txt files, and then generate new makefiles (or whatever) on other machines or in other directories when you check it out. You can even have different people on the team using different build files - one person using makefiles, one person using eclipse+ninja, etc, all generated by cmake.

How do I create one specific directory with Automake?

I just want to know what do I need to write in the Makefile.am to create a directory called build in the same directory where Makefile.am is.
Think about your question carefully: Do you really want to create build in the same directory as Makefile.am, or in the current working directory when configure is called? These are not always the same thing: the GNU build system is meant to support out-of-tree builds (with a potentially read-only $srcdir), so the end user should expect the following to work:
$ tar xf autofoo-1.2.tar.gz
$ mkdir autofoo-build
$ cd autofoo-build
$ ../autofoo-1.2/configure
$ make
$ sudo make install
Now, the easiest way I have found to create a directory is not to use Makefile.am at all, but instead to make config.status do it (which is the shell script that does all of the template substitutions at the end of configure, turning Makefile.in into Makefile, config.h.in into config.h and so on). In configure.ac, put the following:
AC_CONFIG_COMMANDS([mkdir], [$MKDIR_P build])
You would need to write:
build:
test -d ${srcdir}/build || mkdir ${srcdir}/build
but you really do not want to do that. The source directory should be considered read-only. If you simply want to create a directory named build in the build directory, just do
build:
test -d build || mkdir build

How do I tell cmake where to output its build data?

How do I tell cmake where it should output its build data?
Let's say I have a dir with the source code called src/,
and then since cmake outputs a lot of files I would like him to put all of that in
a dir called build/.
BUT I would like him to put the generated Makefile in the project root,
so I don't have to go into the build dir to build the application.
Is this possible with cmake?
I have managed to get the cmake out put if I fun cmake in the build dir like this:
cd build/
cmake ../src/
make
./hello
But it would be nice to stay in the project root and type something like this
cmake
make
./hello
I guess that I need to put a CMakeList.txt in the project root with some magic commands telling him where he could put the object files and where he can find the source code.
Thanks
Update:
Since my question is a little bit vague.
After I have run the cmake commands this is how I would like my tree to look like:
src/CMakeLists.txt
src/hello.c
src/hello.h
build/CMakeCache.txt
build/CMakeFiles/
build/cmake_install.cmake
CMakeLists.txt
Makefile
So the question is how should the CMakeLists.txt look like in this setup.
CMakeLists.txt
src/CMakeLists.txt
But maybe that is not possible?
BUT I would like him to put the generated Makefile in the project root, so I don't have to go into the build
dir to build the application.
cmake not designed for that, as I know,
BUT you can stay in the project root and type:
make -C build
./hello
with custom build rules or set_target_properties,
you can force cmake to put result executable to
sources directory or you can use
./build/hello
Type "cd build && cmake .." you need only once,
after that make will automaticaly start cmake, if something
changed.
cmake wants you to have a fresh build directory.
Okay, i get what you want. I think, you can achieve this with some machinery in CMakeLists.txt. Of course, it's not option if you are not project developer.
In root CMakeLists.txt you can add file(WRITE ...) command, which would write Makefile into ${CMAKE_SOURCE_DIR}. This Makefile would contain these commands for every target:
<target>:
cd ${CMAKE_BUILD_DIR} && ${CMAKE_MAKE_PROGRAM} <target>
So, now you can run make from source dir and it will build your project in build dir.
Simply use
cmake .
make
in your src directory. The (.) dot on unix systems addresses the current directory. Keep in mind doing so is actually not recommended since there will be a lot of build files in your src directory you'll have to clean up afterwards or at release time.