how to compile from src directory to bin directory? - compilation

I have a src directory that contains main.ml and compose.ml files, and i need to compile from that directory and store the binary result in bin directory.
How can i make it?

I am assuming here that main.ml is the main program and compose.ml a module it uses. Then the compilation process will produce files compose.cmo (or compose.cmx), compose.cmi, and the final binary.
It is not quite clear, where you want the intermediate files (compose.cmo and compose.cmi) to go.
If you are fine with them being in src, you can use the -o command line option in the final compilation step.
If you want to keep the source directory clean but are fine with extra stuff going to bin, I suggest symlinking your source files to bin and compile there.
If you want to keep both src and bin clean, you can use a third directory like build, where you symlink the sources, compile, and copy the final binary to bin.

The easiest solution is probably to use ocamlbuild:
ocamlbuild -I src -no-links main.native
ln -f _build/src/main.native bin/main
This will leave the temporary files in the _build directory, which you can later remove with ocamlbuild -clean or rm -r _build.
Use main.byte instead of main.native instead if you want to build the bytecode version.

Related

change prerequisites in makefile at runtime

I am relatively new to make and don't know how to do one specific thing:
The overall process should look something like this:
the source files are java sources in a directory like src/org/path/to/packages/*.java
I want only to translate a specific java file, but the translation process will automatically translate all dependencies (I say 'translate' because I use j2objc to translate the java files to obj-c files - but that should be of no concern for this question)
The translated files will be put into the build/ directory with a folder structure reflecting the source folder structure (so build/org/path/to/packages/.m+.h)
These *.m and *.h files will then be compiled with j2objcc (a clang wrapper) into *.o files -> this step has to be done per file so every file is compiled with a command like j2objcc -c build/org/path/to/packages/file1.m -o build/org/path/to/package/file1.o
these shall be combined into a static library using ar
My problem is that I know which (one) java file I am starting with, but after step 2 I don't know which *.m and *.h files are generated/translated into the build directory. I'd like to read the contents of the build dir after step 2 with a command like find ./build -name '*.m' at make runtime but I don't know how to use this as a prerequisite in the make target.

makefile how can I generate obj files in a subfolder?

I want to generate my obj files in a subfolder, I have tried this:
lib/*.o: source/*.cpp
clang++ $(CC_FLAGS) -c -Iinclude source/*.cpp
But it still generates the obj files in the project root and not in the lib/
The project tree that I'm trying to have:
Project/
source/(cpp files)
include/(header files)
lib/(obj files)
You don't show your current makefile, but my suspicion is that it's wrong. However as we can't see it, we'll leave that alone.
The compiler does not support writing multiple object files to a different directory. If you pass multiple source files along with the -c flag then it will write out multiple object files but only to the current directory... as you've discovered the -o flag can't be specified on compile lines which generate multiple output files.
You can change your recipe to look like this:
cd lib && clang++ $(CC_FLAGS) -c -I../include ../source/*.cpp
this will cause all the object files to be written to the lib directory because it's now the current directory.
However, putting this into a makefile is not simple, because make itself is designed to have a single recipe create a single target. However you have this problem with your existing makefile which you don't show, as well.

How do you specify that a FreeBSD Makefile place the objects (*.o) into a different directory?

If I have some source files in another directory and I want to make some libraries using the source files, I want to have the *.o, *.po, and *.So files in the local directory. It isn't clear how to accomplish this. The transformation rules in bsd.lib.mk always point the .o into the source directory.
How do I get the results of the ${CC} -c to be in the local directory?
The file bsd.lib.mk inclused bsd.obj.mak, so you can use the MAKEOBJDIR environment variable.
Edit: If you cannot control how make is called, then don't use bsd.lib.mk and make explicit dependencies in your Makefile;
foo.o: ../bar/foo.c

How to have automake install headers file in a subdirectory of include

My current project has a couple files in its include/tatoparser directory that I would like to distribute along with my library.
qdii#nomada ~/MyProject $ ls include/tatoparser/
dataset.h interface_lib.h linkset.h sentence.h tagset.h
I created a Makefile.am that references those files:
qdii#nomada ~/MyProject $ cat include/Makefile.am
include_HEADERS = tatoparser/interface_lib.h tatoparser/sentence.h tatoparser/dataset.h tatoparser/tagset.h tatoparser/linkset.h
But when I run make install the referenced files get copied into /usr/include (or whatever $(includedir) was set to), and I want them copied in /usr/include/tatoparser.
How can I do that?
Use the nobase prefix to stop Automake from stripping the subdirectory path from the files:
nobase_include_HEADERS = tatoparser/interface_lib.h tatoparser/...

Search current directory last when looking for dependencies using GNU make and VPATH

This is a GNU Make dependency resolution issue.
My problem is that I copy the source files from a remote file server to a scratch disk (which speeds up the build process by 50%). If the file copy fails, I want to use the source files from the file server, else I want to read them from the scratch disk.
I have tried to use the vpath mechanism, but the problem is that, as far as I understand, make will by default start looking for the source files in the current directory and only if it fails to find the files there, look in the directories listed with vpath.
Is it possible to have make first look in the vpath directories before looking in the current directory? Or perhaps only look in the vpath directories (and explicitly and dynamically add the current directory to vpath)?
Only way I can think of is to temporary change directory so that make always will fail to find the source files.
Look at the path to the source directory on the server. Suppose it's "/server/someplace/src/". And suppose you don't have a "src" directory in the current directory (if you do, we just have to tweak this method). Just make sure that the path to the source directory on the scratch disk ends in "/src/", such as "/scratch/wednesday/src/". Then you can do this:
SCRATCH_PATH = /scratch/wednesday/
SERVER_PATH = /server/someplace/
VPATH = $(SCRATCH_PATH) $(SERVER_PATH)
%.o: src/%.cc
$(CC) blah blah blah

Resources