Skip to content

Instantly share code, notes, and snippets.

@caiorss
Created October 4, 2020 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caiorss/b411be3b2a4364c7d4de18edd37da6a3 to your computer and use it in GitHub Desktop.
Save caiorss/b411be3b2a4364c7d4de18edd37da6a3 to your computer and use it in GitHub Desktop.
Embedded Linux-ARMV7 Cross Compilation with Musl-LibC
cmake_minimum_required(VERSION 3.9)
project(embedded-linux-armv7)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
#====== Global compiler and linker settings ======#
# (-static) => Fully statically link Musl-LibC
# (-Wl,--gc-sections) => Remove unused code
# (requires: -fdata-sections and -ffunction-sections)
set(CMAKE_EXE_LINKER_FLAGS "-static -Wl,--gc-sections")
# Separate data and code sections in order to reduce binary size.
add_definitions( -fdata-sections -ffunction-sections )
# Copy target executable, for instance my-exe to my-exe.debug
# and strip debugging symbols from my-exe.
#
# Reference: https://cmake.org/pipermail/cmake/2011-October/046743.html
macro(STRIP_DEBUG_SYMBOLS target)
ADD_CUSTOM_COMMAND(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${target}> ${CMAKE_BINARY_DIR}/${target}.debug
COMMAND ${CMAKE_STRIP} -g $<TARGET_FILE:${target}>
)
endmacro()
#========== Targets Configurations ============#
add_executable( app portable-app.cpp )
target_link_libraries( app stdc++fs)
STRIP_DEBUG_SYMBOLS( app )
FROM muslcc/i686:armv7l-linux-musleabihf
RUN apk add make cmake
# ENTRYPOINT ["cmake"]
DOCKER_COMMAND=docker run -e UID=$(shell id -u) -e GID=$(shell id -g) --volume=$(shell pwd):/cwd --workdir=/cwd musl-armv7
# Builds docker image: musl-armv7
docker:
docker build -f Dockerfile.docker -t musl-armv7 .
# Build testing application
build:
${DOCKER_COMMAND} cmake -H. -B_build-armv7 -DCMAKE_BUILD_TYPE=Debug
${DOCKER_COMMAND} cmake --build _build-armv7 --target
clean:
rm -rf -v _build-armv7
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <filesystem>
#include <fstream>
#include <cassert>
namespace fs = std::filesystem;
int main(int argc, char** argv)
{
assert( argc >= 2 && "Supposed to have at least one arguments");
std::cout << std::boolalpha;
std::string command = argv[1];
if(command == "version")
{
auto ifs = std::ifstream("/proc/version");
assert( ifs.good() && "File supposed to exist" );
std::cout << ifs.rdbuf();
return EXIT_SUCCESS;
}
std::cout << "\n ===== Listing directory /etc =====" << std::endl;
if(command == "list")
{
// Show first 10 files of directory /etc
auto iter = fs::directory_iterator(argv[2]);
std::for_each(fs::begin(iter), fs::end(iter)
,[](auto p){
auto path = p.path();
std::cout << std::left
<< std::setw(0) << path.filename().string()
<< " " << std::setw(35)
<< std::right << std::setw(40) << path
<< std::endl;
});
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment