Store GNU make generated files elsewhere - linux

How can I store GNU make & configure files elsewhere? I have a project I am working on that I get compiled using:
./configure --prefix=/usr && make && su -c 'make install'
The thing is I don't want to pollute the current folder, which is a svn/git/hg/whatever sandbox with files generated by that command. I want to put those files in a separate location. I don't know if it's similar, but when I compile the linux kernel from source, I can specify where to put the ouput by passing the 'O' option to 'make', something like this:
make O=/home/user/linux-output

The Makefile must support this feature.
I think the autoconf generated makefiles all support the following use:
mkdir ../build
cd ../build
../configure --prefix=/usr
make
make install
(It's certainly recommended for gcc builds).

As Kristof already pointed out, GNU autotools inherently support out out-of-tree builds at the configure level.
So you can have the Makefile and built binaries out of the source tree trivially.
To get all the auto-generated artefacts out of the source tree requires much more work however.
We have a script that copies changes from a source tree into a working_copy, carefully preserving the configure script etc in the working_copy, which allows the original source tree to be pristine. However it's very inefficient, so I wouldn't recommend it.
I would recommend a normal out-of-tree build, and then explicitly excluding the remaining auto-generated files in the source tree.

Related

Building an out-of-tree kernel module with separate source and build directories via autoconf

Given a package that uses autoconf (a configure script) to provide both a user-mode application and a kernel driver, with a directory structure like this:
configure.ac
configure
Makefile.am
user
Makefile.am
user.c
driver
Makefile.am
Kbuild.in
driver.c
This successfully configures and builds both user and driver when configured locally (run ./configure --options).
However a useful feature of configure scripts is that you can have a separate source and build directory, simply by running the script from the build directory, eg:
mkdir build && cd build && ../configure --options
This works fine for the user app, but I'm having trouble getting the kernel driver to build in this context. In the driver's Makefile.am I have the following, as suggested in this answer:
modules:
$(MAKE) -C "$(LINUX_SOURCE_DIR)" M="#abs_builddir#" src="#abs_srcdir#" modules
But this fails to compile, as it's expecting the Kbuild or Makefile to be in src, but they're actually in M. Omitting src does let it find the Kbuild but it then fails to find the actual source files (no rule to make target //path/in/build//driver.o).
Using O= as some other answers suggest is not correct, as this specifies the path to find the kernel's output files (.config etc) and these are still in the LINUX_SOURCE_DIR. (So trying to use it results in various other missing-file errors.)
I'm hoping for a solution that doesn't require modifying any actual Kbuild scripts, since those are outside my package's control.
Thus far the best option I've come up with is to copy/link all the module source files into the build directory before calling Kbuild, which is not ideal. I'm hoping there's a hidden setting that can be used for this.

How to build ld.so alone?

I know that ld.so belongs to glibc, and I want to modify the source code of it and build it alone. But building entire glibc is too much heavy, and the makefiles are so complicated that I just don't know where to start digging out. Any tips?
I want to modify the source code of it and build it alone
If you did ./configure; make in the source directory (i.e. you are not using a separate build directory), then:
make -C elf
will rebuild ld.so. If you build in a separate directory, e.g. in /tmp/build while your source is in $HOME/glibc-src, then:
cd /tmp/build; make -C $HOME/glibc-src/elf objdir=`pwd`
If you have a reasonably powerful machine, make -j16 ... may work even better.

Compile GCC and install to DESTDIR

I'm trying to install GCC into /my/custom/path/gcc
but for some reason it installs into the normal installation path.
the commands i'm using:
configure --target=i686-pc-linux-gnu --disable-nls --enable-languages=c,c++ --without-headers
make DESTDIR=/my/custom/path/gcc
make DESTDIR=/my/custom/path/gcc install
What am I doing wrong?
You should run (in a new build tree outside of the source tree)
/your/source/path/to/gcc/configure --target=i686-pc-linux-gnu --prefix=/my/custom/path/gcc ...
and then GCC will become installed in /my/custom/path/gcc/bin/ with include files in /my/custom/path/gcc/include/, libraries in /my/custom/path/gcc/lib/ etc etc
I suggest using /opt/ or $HOME/pub as your prefix and you might also be interested by the --program-suffix=-foo option
(do that in a fresh new build tree outside of the source tree; your previous one is rotten)
After successive compilation with make, you can run in your build tree
make install DESTDIR=/tmp/mygccinst/
and finally, you can copy the definitive files with something like
cp -va /tmp/mygccinst/ /
You may need to run this copy as root...
PS the installation prefix is built-in the gcc driver binary, which actually runs cc1 or cc1plus etc...

How to recompile just a single kernel module?

Usually kernel source are stored in /usr/src/linux-2.6.x/.
To avoid to recompile the entire kernel if I modify a module's source, how can I recompile just that module?
Switch to the root directory of your source tree and run the following command:
$ make modules SUBDIRS=drivers/the_module_directory
And to install the compiled module:
$ make modules_install SUBDIRS=drivers/the_module_directory
Note: As lunakid mentions, the latter command might not build the module first, so be careful.
since kernel versions 3.x.x and 4.x.x the procedure gets more complicated (but there is a hope, so keep reading):
make distclean if you haven't just cloned a new source but used to build other modules before
create new folder somewhere for the module source (example: extra)
and copy only source files (from the kernel source or somewhere else) related to the module needed to be build into this new folder
copy /boot/config-`uname -r` file (example: /boot/config-4.8.0-46-generic) into kernel source folder file .config and run make oldconfig. if the module belongs to the kernel source, verify if it has been enabled by calling make menuconfig, by searching for the module and applying letter 'M' if necessary
kernel source root Makefile has to be altered with exact version components matching the current running one (you may verify with make kernelversion if it matches exactly the uname -rone)
there is been a strong suggestion to build scripts also before with
make scripts
make prepare and make modules_prepare has to be executed prior to the actual module build
Module.symvers has to be copied from the target system headers folder corresponding running kernel version /usr/src/linux-headers-`uname -r`/Module.symvers (example: /usr/src/linux-headers-3.13.0-117-generic/Module.symvers) into the newly created module source files folder prepared for the module compilation (the one extra in example).
create new Makefile inside module source compilation folder having following line: obj-y += <module_source_file_name>.o or if the source code is complicated, use the guidance from here
only then it's the right time to build module with make -C <kernel source path> M=the_module_directory (example: make -C . M=extra/)
Use command modprobe --dump-modversion <module_name>.ko to verify CRC match between module exporting API and corresponding values in Module.symvers. in case of failure use command modinfo <module_name>.ko instead
verify if kernel.release file content match exactly the one from headers of the current running version. if you'll discover + appended at the end, it means you've been compiling git clonned source and your experimental modifications caused build system to compromise the localversion string by adding + at the end.
if only + has been discovered at the tail of kernel.release stored value and it's a mismatch with the exact name of the target running kernel,
the solution would be following:
commit all your changes, force release tag to shift above your modifications with the git tag -a <tag version> -f command. then rebuild your modules from step 8
You can pass the path to the module name or module directory to make as parameter.
make path/to/the/module/itself.ko
make path/to/the/module/directory/
In case you have edited just code in drivers/net/ethernet/intel/e1000/e1000_main.c file
Build the module.
make scripts prepare modules_prepare
make -C . M=drivers/net/ethernet/intel/e1000
Install the module.
cp drivers/net/ethernet/intel/e1000/e1000.ko /lib/modules/5.1.15/kernel/drivers/net/ethernet/intel/e1000/e1000.ko
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules
make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
https://askubuntu.com/questions/515407/how-recipe-to-build-only-one-kernel-module

Compiling ODE code in Linux

I have downloaded ode-0.11.1 and I am able to compile source code by modifying the Makefile provided with the demos, but I can't figure out how to manually compile and link the code I need as a standalone.
I tried probing at the Makefile and substituting the macros manually, as well as running it and checking the output.
Does anyone know how to do this?
OpenDE, or ODE, is configured with GNU autoconf/automake tools.
This means that you need to invoke ./configure before you can build the package.
To see all the options, use:
$ ./configure --help
An invokation to ./configure will often need a target where to install to.
A typical invokation would look like this:
$ ./configure --prefix=$HOME --with-trimesh=opcode
$ make install
This will install OpenDE in your home directory.
If you omit the --prefix argument, the default /usr/local will be used, and you will need root permissions to install.
The example invokation will also include support for triangle meshes using OPCODE.

Resources