How to inject variables into u-boot build at build time - c

I am a u-boot newbie.
I want to be able to make u-boot with a different build option at build time. E.g. I might have a build.sh like this:
source /path/to/target-toolchain
make distclean
make MyHardware_defconfig
make V=1
or even better:
make distclean
make MyHardware_defconfig
make CONFIG_ENABLE_CONSOLE=1 V=1
In MyHardware.c:
#if !defined CONFIG_ENABLE_CONSOLE
#error CONFIG_ENABLE_CONSOLE not defined!!!!!
#endif
I tried adding CONFIG_ENABLE_CONSOLE=1 to MyHardware_defconfig but when build get error line
I also tried make CONFIG_ENABLE_CONSOLE=1 V=1 but also same as above error.
How can I setup my project so I can build for both console enabled and disabled? Without having to hard code in the u-boot source code.

make MyHardware_defconfig
creates file .config. The usual way to edit it is calling
make menuconfig
If you want to modify .config in a script, you could use sed.
The configuration options are defined in files called Kconfig. https://www.kernel.org/doc/html/v5.18/kbuild/kconfig-language.html describes the syntax of Kconfig files.

Related

Make clangd aware of macros given from the compiler

I have two executables that are build from the same source (a client and a server) and they're built with the compile options -D CLIENT=0 -D SERVER=1 for the server and -D CLIENT=1 -D SERVER=0 for the client. If I do something like
if (CLIENT) {
// Client specific code
}
clangd complains that CLIENT is not defined. Is there a way to make clangd aware of those macros? (The code compiles just fine, the errors are from clangd, not the compiler)
Is there a way to make clangd aware of those macros?
From getting started with clangd:
Project setup
To understand source code in your project, clangd needs to know the
build flags. (This is just a fact of life in C++, source files are not
self-contained.)
By default, clangd will assume that source code is built as clang
some_file.cc, and you’ll probably get spurious errors about missing
#included files, etc. There are a couple of ways to fix this.
compile_commands.json
compile_commands.json file provides compile commands for all source
files in the project. This file is usually generated by the build
system, or tools integrated with the build system. Clangd will look
for this file in the parent directories of the files you edit. Other
tools can also generate this file. See the compile_commands.json
specification.
compile_commands.json is typically generated with CMake build system, but more build systems try to generate it.
I would suggest moving your project to CMake, in the process you will learn this tool that will definitely help you in further C-ish development.
compile_flags.txt
If all files in a project use the same build flags, you can put those
flags, one flag per line, in compile_flags.txt in your source root.
Clangd will assume the compile command is clang $FLAGS some_file.cc.
Creating this file by hand is a reasonable place to start if your
project is quite simple.
If not moving to cmake, create a compile_flags.txt file with the content for example like the following, and clangd should pick this file up:
-DCLIENT=1
-DSERVER=1

Automake error './ltmain.sh' not found

I've installed mingw and msys by using mingw-get-setup.exe. I've also installed Autotools(autoconf, automake,m4,libtool) into C:\/opt/autotools.
When I run automake, the following error always occurs:
configure.ac:11: error: required file './ltmain.sh' not found
If I copy ltmain.sh from libtool’s installed tree, execution will finish normally.
How can I configuure automake to find ltmain.sh without copying?
In an autoconf/automake/libtool project you need to run:
libtoolize: this copies/links a few support scripts, including ltmain.sh (which is the main component of libtool).
aclocal: this looks up all m4 macros that your configure script will need, and make a local copy for easier access.
autoheader: optional, if you want to use config.h/AC_CONFIG_HEADERS, otherwise all the test result macros will be inlined when you call the compiler.
autoconf: to expand all the macros used by configure.ac into the configure script.
automake: to convert all the Makefile.am into Makefile.in templates. You probably want to invoke this with --add-missing so additional support scripts can be linked/copied to your project (such as compile, missing, depcomp, test-driver, etc).
Don't worry about running each tool. Just invoke autoreconf -i and it'll run the tools that are needed. Add -v if you want to see what tools is being executed. To avoid mistakes, just put a script like this at the root of your project:
#!/bin/bash -x
mkdir -p m4
exec autoreconf --install "$#"
Users that checkout/clone the project directly from the source repository will need to run this ./bootstrap script at least once. This is not needed if the user got a tarball distribution.
Automake can take fairly good care of itself; it'll re-invoke the above tools when needed, when you run make. But if you generate a broken Makefile, you'll need to invoke ./bootstrap and ./configure again to generate new Makefiles.
As DanielKO stated, ltmain.sh is created by libtoolize.
However, what if it doesn't?
The following requirements need to be met:
configure.ac must exist and contain at least one of:
AM_PROG_LIBTOOL,AC_PROG_LIBTOOL,LT_INIT
(see function func_require_seen_libtool in /usr/bin/libtoolize)
If configure.ac does not contain a AC_CONFIG_AUX_DIR, libtoolize will look for a file called 'install-sh' or 'install.sh' in ., .. and ../.. and if found use that as "auxdir" and install ltmain.sh there (see function func_require_aux_dir inside libtoolize).
In my case, I was working on an "example project" in a subdirectory of another project, and the example project did not have a AC_CONFIG_AUX_DIR in its configure.ac; therefore libtoolize found the root of the parent project and installed ltmain.sh there instead of in the example project's root.

Compile C code on Mac

I am trying to compile this interesting program on my mac. The program can make your terminal looks like the one in Matrix movie. Here is the link.
But when I type "make" and press enter in the upzipped source code folder, here is the error:
make: *** No targets specified and no makefile found. Stop.
I am not very familiar with things that need to compile. I know the concept of compilation and c/c++, but have little practical experience. Is it possible to compile this code on mac? If it is easy, can you tell me how I can compile this code from the above link?
Go to the folder of your source code with terminal and type ./configure and hit enter. Then call make.
The package uses the autoconf tools to generate the Makefile(s) required to build the cmatrix application.
In order to generate the makefiles, you need to run the configure script:
$ ./configure
If you have a look at the INSTALL file, it should provide some additional information, such as additional arguments that you may need to pass into the configure script for your platform.
Once the configure script has run, you can run make as normal, and it should compile the application.

How to use libnet with cmake (and kdevelop)?

I am afraid the question I have might be stupid, but as I am new to kdevelop and cmake it is quite hard for me to understand how they work.
The project I tried to set up uses the libnet 1.1 library. My Question is how do I get cmake to compile and link this library so I can use it properly?
Here is what I already tried:
PROJECT(test)
include_directories("${PROJECT_SOURCE_DIR}/libnet")
add_subdirectory(libnet)
ADD_EXECUTABLE(test main.c)
target_link_libraries(test libnet)
Thank you for your Help!
It looks like libnet does not use CMake itself, so you're going to have to build it separately or make it part of your own project.
To build it separately, you have a couple of choices. You can build it (and install it if you want) and then use find_library to locate the actual libnet.a / libnet.lib file.
find_library(libnet NAMES net libnet PATHS <wherever you built it to>)
include_directories(<wherever you built it to>/include)
target_link_libraries(test libnet)
CMake provides a decent way to automate this through use of ExternalProject_Add. This is a little trickier to use, but you can make it download, extract, build and install libnet all in one command. It looks like libnet has several different ways of being built though, depending on platform, so this may not be too straightforward.
Another option would be to include the libnet sources in your own project and add it as a library via add_library. You'd need to create a list of the libnet sources, and also examine the libnet makefiles to check for any compiler flags / oddities that would need special handling in your own CMakeLists.txt
This is perhaps the best option since it gives you access to the full libnet source tree in your IDE, allows you to fine-tune the libnet build, and causes your own project to go out of date (need rebuilding) if the libnet build changes.
set(LibnetSources <list all sources and headers>)
add_library(libnet ${LibnetSources})
include_directories(${PROJECT_SOURCE_DIR}/libnet/include)
target_link_libraries(test libnet)
You can make use of file(GLOB...) to help with generating the list of libnet sources, but it's not recommended since the addition or removal of a file would not be automatically detected by CMake. You need to make sure that if you do this, you re-run cmake manually before trying to recompile. This isn't an issue if you're not planning on adding/deleting any libnet files.
Edit: Use ExternalProject Module
OK, there is a third option which is maybe the best, but can be slightly complex to set up; use CMake's ExternalProject Module. This is designed to allow building of external dependencies - even ones which don't use CMake. This is a decent article on using it.
Try replacing your CMakeLists.txt with this (only tested on Ubuntu with gcc). In short, it downloads libnet, configures it, builds it and installs it to your build tree (not to /usr/local). Your executable can then include and link to it.
# Minimum version 2.8.5 since we need ExternalProject module
cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
project(test)
# Enable ExternalProject CMake module
include(ExternalProject)
# Set default ExternalProject root directory
set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/ThirdParty)
# Add libnet
ExternalProject_Add(
libnet
URL http://packetfactory.openwall.net/libnet/dist/libnet.tar.gz
TIMEOUT 30
CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR>
BUILD_IN_SOURCE ON
# Wrap download, configure, build and install steps in a script to log output
LOG_DOWNLOAD ON
LOG_CONFIGURE ON
LOG_BUILD ON
LOG_INSTALL ON)
# Specify include dir
ExternalProject_Get_Property(libnet install_dir)
include_directories(${install_dir}/include)
# Add test executable target
add_executable(test main.c)
# Create dependency of test on libnet
add_dependencies(test libnet)
# Specify test's link libraries
target_link_libraries(test ${install_dir}/lib/libnet.a)

How do I detect a debug build in my c source files with cmake?

I have this set in my CMakeLists.txt file.
SET (CMAKE_BUILD_TYPE "Debug")
However, this is not working in my C source files
#if defined(DEBUG)
// not getting here
#else
// getting here instead
#endif
What symbol if any are defined by setting CMAKE_BUILD_TYPE to Debug ?
Found the answer. It's definately a trap for those new to using cmake.
It seems that changing the build type in CMakeLists.txt will not change the build type if you've built it previously with another build type. The reason is because of it's cache.
Seeing as I am building out of source. i.e. I have a separate Build directory inside my source tree. I normally would run just "cmake .."
So clearing the Build tree and running "cmake .." again fixed it.
Now DEBUG is being defined for my source files and I can verify it with make VERBOSE=1
This link provides further detail and other options:
Understanding why CMAKE_BUILD_TYPE cannot be set

Resources