How to set LOCAL_LDFLAGS/LOCAL_CPPFLAGS with cmake in ndk? - gradle

How to set LOCAL_CPPFLAGS/LOCAL_CFLAGS/LOCAL_LDFLAGS with cmake? I want to reduce so size, but the tutorials i have read are all about mk files. What should I do in cmakelists.txt?
I directly set LOCAL_CPPFLAGS/LOCAL_CFLAGS/LOCAL_LDFLAGS, but it seems not work.
set(LOCAL_CPPFLAGS "${LOCAL_CPPFLAGS} -ffunction-sections,-fdata-sections")
set(LOCAL_CFLAGS "${LOCAL_CFLAGS} -ffunction-sections,-fdata-sections")
set(LOCAL_LDFLAGS "${LOCAL_LDFLAGS} -Wl,--gc-sections,--icf=safe")

You should be able to do that using something like this:
target_compile_options(mytarget PRIVATE -ffunction-sections -fdata-sections)
target_link_libraries(mytarget -Wl,--gc-sections,--icf=safe)
Note that -ffunction-sections has been enabled by default for a while when using the NDK's Clang. And if you're using NDK r19c or later I believe -fdata-sections is enabled by default too. So only the linker flags should be necessary for you to specify explicitly.

Related

Buildroot gcc headers don't match linux-headers

I'm using Buildroot 2018.02.7 to build a simple Linux system for i386 PC, as a precursor to doing the same thing for an embedded ARM system. I keep running into problems like this one, in building the util-linux module:
CC lib/libcommon_la-path.lo
lib/pager.c:11:17: fatal error: err.h: No such file or directory
#include <err.h>
^
compilation terminated.
Makefile:8596: recipe for target 'lib/libcommon_la-pager.lo' failed
make[3]: *** [lib/libcommon_la-pager.lo] Error 1
When I look in the linux-headers source tree in .../output/build/linux-headers-4.13.8, the file is found. But Buildroot is pointing to a different set of headers, the one built into its GCC:
devuser#3faf730b4a1b:~/pc/buildroot-2018.02.7/output/build/util-linux-2.31.1$ ../../host/bin/i686-buildroot-linux-uclibc-gcc -print-sysroot
/home/devuser/pc/buildroot-2018.02.7/output/host/i686-buildroot-linux-uclibc/sysroot
devuser#3faf730b4a1b:~/pc/buildroot-2018.02.7/output/build/util-linux-2.31.1$
And the two /usr/include subdirectories are significantly different.
Have I missed a configuration setting somewhere in the defconfig file? This is the defconfig:
BR2_x86_core2=y
BR2_SSP_REGULAR=y
BR2_PACKAGE_HOST_LINUX_HEADERS_CUSTOM_4_13=y
BR2_UCLIBC_CONFIG="board/pc/dsa_pc_i386_uclibc.config"
BR2_TOOLCHAIN_BUILDROOT_USE_SSP=y
BR2_TOOLCHAIN_BUILDROOT_CXX=y
BR2_TARGET_GENERIC_GETTY_PORT="tty1"
BR2_ROOTFS_POST_IMAGE_SCRIPT="board/pc/post-image.sh support/scripts/genimage.sh"
BR2_ROOTFS_POST_SCRIPT_ARGS="-c board/pc/genimage-bios.cfg"
BR2_LINUX_KERNEL=y
BR2_LINUX_KERNEL_CUSTOM_VERSION=y
BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="4.13.8"
BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/pc/linux.config"
BR2_LINUX_KERNEL_INSTALL_TARGET=y
BR2_LINUX_KERNEL_EXT_XENOMAI=y
BR2_PACKAGE_BUSYBOX_CONFIG="board/pc/dsa_pc_i386_busybox.config"
BR2_PACKAGE_LINUX_FIRMWARE=y
BR2_PACKAGE_LINUX_FIRMWARE_ATHEROS_9170=y
BR2_PACKAGE_LINUX_FIRMWARE_ATHEROS_9271=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_3160=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_3168=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_5000=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_6000G2A=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_6000G2B=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_7260=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_7265D=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_8000C=y
BR2_PACKAGE_LINUX_FIRMWARE_IWLWIFI_8265=y
BR2_PACKAGE_LINUX_FIRMWARE_RALINK_RT73=y
BR2_PACKAGE_LINUX_FIRMWARE_RALINK_RT2XX=y
BR2_PACKAGE_LINUX_FIRMWARE_RTL_81XX=y
BR2_PACKAGE_LINUX_FIRMWARE_RTL_87XX=y
BR2_PACKAGE_LINUX_FIRMWARE_RTL_88XX=y
BR2_PACKAGE_LINUX_FIRMWARE_RTL_8169=y
BR2_PACKAGE_ACPID=y
BR2_PACKAGE_DBUS=y
BR2_PACKAGE_ZLIB=y
BR2_PACKAGE_LIBFFI=y
BR2_PACKAGE_PCRE=y
BR2_PACKAGE_PCRE_UCP=y
BR2_PACKAGE_READLINE=y
BR2_PACKAGE_WPA_SUPPLICANT=y
BR2_PACKAGE_WPA_SUPPLICANT_DBUS_NEW=y
BR2_PACKAGE_KMOD=y
BR2_PACKAGE_UTIL_LINUX=y
BR2_PACKAGE_UTIL_LINUX_LIBMOUNT=y
BR2_TARGET_ROOTFS_EXT2=y
BR2_TARGET_ROOTFS_EXT2_4=y
BR2_TARGET_ROOTFS_EXT2_SIZE="120M"
# BR2_TARGET_ROOTFS_TAR is not set
BR2_PACKAGE_HOST_GENIMAGE=y
Enable UCLIBC_HAS_BSD_ERR in uClibc.
You have a custom uClibc configuration. That falls squarely in the "you know what you are doing" category, since it allows you to remove features that other packages rely on.
In this case, util-linux relies on the non-Posix err.h include. This is only installed if UCLIBC_HAS_BSD_ERR is enabled in the uClibc configuration.
There is a high risk of running into similar issues with a custom uClibc configuration.

Converting static framework to dynamic

I want to convert static iOS framework (https://github.com/comScore/ComScore-iOS-watchOS-tvOS/tree/master/ComScore/iOS) into dynamic.
> clang -arch x86_64 -dynamiclib -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.3.sdk -lc++ -F . -framework ComScore -ObjC -o ComScoreDynamic
This command is successful, however, there is a problem with symbol visibility.
When I check symbols in the original framework it is around 4k of public symbols:
> nm -gU ComScore.framework/ComScore | wc -l
4387
In dynamic version only very few of them:
nm -gU ComScoreDynamic
0000000000114af8 S _OBJC_CLASS_$_SCORCommonUtils
0000000000114940 S _OBJC_CLASS_$_SCORCrossPublisherIdSourceValue
0000000000114a08 S _OBJC_CLASS_$_SCORHTTP
0000000000114990 S _OBJC_CLASS_$_SCORHelper
0000000000114aa8 S _OBJC_CLASS_$_SCORObfuscation
0000000000114a80 S _OBJC_CLASS_$_SCORReachability
0000000000114918 S _OBJC_CLASS_$_SCORUniqueId
0000000000114b20 S _OBJC_METACLASS_$_SCORCommonUtils
00000000001149e0 S _OBJC_METACLASS_$_SCORCrossPublisherIdSourceValue
0000000000114a30 S _OBJC_METACLASS_$_SCORHTTP
0000000000114968 S _OBJC_METACLASS_$_SCORHelper
0000000000114ad0 S _OBJC_METACLASS_$_SCORObfuscation
0000000000114a58 S _OBJC_METACLASS_$_SCORReachability
00000000001149b8 S _OBJC_METACLASS_$_SCORUniqueId
00000000001166b0 D __ZTINSt3__117bad_function_callE
00000000000d5d60 S __ZTSNSt3__117bad_function_callE
All other symbols marked as internal (t and s markings).
How can I keep symbols external?
UPDATE:
Looks like this can be similar problem: Export an `OBJC_CLASS` from one static lib as part of another
Problem is that symbols in static lib are exported as private_extern and there is no way to preserve them in dynamic library.
The public symbols in the comScore framework are marked private external. This can be seen with the nm utility and looking for the SCORAnalytics class:
nm -m ComScore/iOS/ComScore.framework/Versions/A/ComScore |grep _OBJC_CLASS_\$_SCORAnalytics
Shows:
---------------- (LTO,DATA) private external _OBJC_CLASS_$_SCORAnalytics
This means the symbol can only be linked against once. When Cocoapods performs a pre-link for secondary ('transitive') dependencies, these symbols lose their extern attribute. The idea here is to prevent public symbols from a dependency from leaking into those of another library. The problem is that, for Swift projects, they aren't fully resolved until the final app link; by that point they're no longer available.
The real issue is that the comScore library is a static framework. The best solution would be for comScore to release it as a dynamic framework, but these are only supported by iOS 8 and above; comScore insists on support all the way back to iOS 6. I know.
For now, my solution is to include the comScore framework directly in our Cocoapod and vend it within the Podspec, allowing it to work with both Obj-C and Swift projects. The downside is I have to manually update our Cocoapod every time comScore releases a new version. There would also be symbol conflicts if another pod included comScore, but since our pod is a metrics aggregator that logs to several backends, it's likely to be the only metrics component in use. YMMV.

DSO library symbol seems to have DEFAULT visibility although should be HIDDEN

I have a problem with setting up a visibility of my shared library. I would like to ask for help in resolving my issue as described below:
I have a bunch of source files which I would like to build as shared library:
+Base
|_Parameter
| |_Parameter.h
| |_Parameter_Exception.h
| |_Parameter_Exception.cpp
|_Data
| |_DataInput.h
| |_DataInput_Exception.h
| |_DataInput_Exception.cpp
...
Well there are some more files in there but I think it does not influence my problem description.
I am using CMake in order to build this SHARED library. Here is part of my CMakeLists.txt which deals with building the library.
project( Base )
cmake_minimum_required(VERSION 3.5
set( Base_HEADERS
Parameter/Parameter.h
Parameter/Parameter_Exception.h
Data/DataInput.h
Data/DataInput_Exception.h
)
set( Base_SOURCES
Parameter/Parameter_Exception.cpp
Data/DataInput_Exception.cpp
)
set( LIBRARY_OUTPUT_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
add_library( Base SHARED ${Base_SOURCES} ${Base_HEADERS} )
generate_export_header( ${PROJECT_NAME} EXPORT_FILE_NAME ${PROJECT_NAME}_Export.h )
This setup builds libBase.so shared library without any problem. It is important to mention that I have set some visibility related flags to be used during linking:
set( CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined -fvisibility=hidden -fvisibility-inlines-hidden" )
As configured in Base library CMakeLists.txt partly listed above:
Base_Export.h header containig GCC visibility attributes macros is
generated by CMake. It is then included in my sources listed in the
tree up there.
By default all the symbols shall be hidden as set in
linker flags (-fvisibility=hidden)
For example a piece of Parameter_Exception.h:
#include "Base_Export.h"
class SomeException : public std::exception
{
public:
SomeException( void ) {}
...
};
Please notice I have not set any visibility attribute here (by a macro defined in Base_Export). So I would assume, the symbol of SomeException class shall be hidden to all the DSO "users" (due to -fvisibility=hidden)
But it doesn't seem so. Unfortunately:
Once I use the library, it the exception is usable although the symbol should be hidden. I would expect link failure cause by this code snippet "outside the library":
#include "Parameter/Parameter_Exception.h"
...
try
{
...
}
/* THIS SHOULD FAIL IN LINKING DUE TO UNDEFINED SYMBOL, RIGHT? */
catch( const SomeException & e )
{
...
}
I have also tried to look for the symbol in the libBase.so using the command:
readelf -Ws libBase.so
it lists enormously long text:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 000000000008b7e0 0 SECTION LOCAL DEFAULT 9
....
but what I have noticed here is that in "Vis" column up there there is always DEFAULT which implies a default visibility ( __attribute__((visibility("default"))) ) to me BUT I would expect to see HIDDEN in there ( __attribute__((visibility("hidden"))) )
So what am I doing wrong? Or my understanding is not correct? I know all the linked flags are propagated correctly but seem to have no effect.
My toolchain configuration as listed by CMake configuration script (GCC version that supports visibility attributes):
...
-- The CXX compiler identification is GNU 5.3.1
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
...
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
-- Configuring done
-- Generating done
Many thanks in advance to anybody willing to help me and possibly others having the same trouble as I do.
Thanks again, Martin
CMake has direct support for symbol visibility handling, so you shouldn't need to touch CMAKE_SHARED_LINKER_FLAGS or CMAKE_CXX_FLAGS at all. You can set the visibility on a per-target basis like this:
set_target_properties(Base PROPERTIES
C_VISIBILITY_PRESET hidden
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
)
CMake will then ensure the relevant flags are added for you. Rather than having to set these properties for each target, it is usually more convenient to set the associated variables to set the defaults for all subsequently created targets (and this is what I generally recommend and use in projects I work on):
cmake_minimum_required(VERSION 3.5)
project(Base)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
add_library(Base SHARED ...)
When used in conjunction with the GenerateExportHeader module (which you already appear to be using), you get support not just for gcc/clang, but also Visual Studio, making it a nice cross-platform way of controlling symbol visibility. You don't need to know the relevant compiler flags or attributes, CMake will handle them for you and give you equivalent behavior across all the compilers.
-fvisibility=hidden and -fvisibility-inlines-hidden are compiler options,
not linker options. Set them in CMAKE_CXX_FLAGS

How to run gcov on test application with Nuttx OS on STM discovery board?

Setup:
Toolchain: gcc-arm-none-eabi-5_2-2015q4-20151219
Target: STM429i-disco board
I want to run gcov and get real time report generated in target as per below link:
https://mcuoneclipse.com/2014/12/26/code-coverage-for-embedded-target-with-eclipse-gcc-and-gcov/
First, sucessfully have compiled my code (POSIX compliant NUTTX OS) with -fprofile-arcs & -ftest-coverage flags & got generated the .gcno files for my src files.
second, sucesfully have linked with -fprofile-arcs flags enabled and using the libgcov.a file (part of the toolchain) and the final binary is generated.
Now, I dont know what changes are needed in my test application to invoke gcov, generate report & dump report.
Another problem is, gcov functions are with HIDDEN attribute in libgcov.a as below.
9: 00000000 4 FUNC GLOBAL HIDDEN 1 __gcov_flush
9: 00000000 4 FUNC GLOBAL HIDDEN 1 __gcov_init
so, I could not invoke as I need.
Any inputs in getting the .gcda file generated would be of great help.
Can you look for gcov_exit instead? It is similar to __gcov_flush. Typically, it's one of gcov_exit and __gcov_flush that would be there and you can use any.
In case this is not there or is also hidden, you can use this approach which I tried for one of my projects. I picked (and modified for various reasons) the implementation of gcov_exit from gcc source code (of version matching my toolchain) (available at https://github.com/reeteshranjan/libgcov-embedded) and plugged it in my project. With everything else remaining the same (the compiler flags etc.), I was able to then break into gcov_exit and follow the rest of the approach in the blog link you have mentioned.

Why won't codeblocks use my project options?

I did what's needed, right click on the project name, click build options...
Select append target options to project options (I also tried use project options only).
Thing is, I have a define and some compiler flags set but when it compiles it doesn't show them on the command line.
mingw32-g++.exe -L"C:\Documents and Settings\Zirgon\Bureau\GSERVER\projects\miniupnpc" -o ..\..\bin\gserver2.exe obj\Release\adler32.o obj\Release\blocksort.o obj\Release\bz2compress.o obj\Release\bzlib.o obj\Release\CEncryption.o obj\Release\CFileQueue.o obj\Release\CFileSystem.o obj\Release\CLog.o obj\Release\compress.o obj\Release\CPluginManager.o obj\Release\crc32.o obj\Release\crctable.o obj\Release\CSettings.o obj\Release\CSocket.o obj\Release\CString.o obj\Release\CTranslationManager.o obj\Release\CUPNP.o obj\Release\CWordFilter.o obj\Release\decompress.o obj\Release\deflate.o obj\Release\gzclose.o obj\Release\gzlib.o obj\Release\gzread.o obj\Release\gzwrite.o obj\Release\huffman.o obj\Release\infback.o obj\Release\inffast.o obj\Release\inflate.o obj\Release\inftrees.o obj\Release\IUtil.o obj\Release\main.o obj\Release\md5.o obj\Release\randtable.o obj\Release\TAccount.o obj\Release\TLevel.o obj\Release\TLevelBaddy.o obj\Release\TLevelBoardChange.o obj\Release\TLevelChest.o obj\Release\TLevelHorse.o obj\Release\TLevelItem.o obj\Release\TLevelLink.o obj\Release\TLevelSign.o obj\Release\TMap.o obj\Release\TNPC.o obj\Release\TPlayer.o obj\Release\TPlayerLogin.o obj\Release\TPlayerNC.o obj\Release\TPlayerProps.o obj\Release\TPlayerRC.o obj\Release\trees.o obj\Release\TServer.o obj\Release\TServerList.o obj\Release\TWeapon.o obj\Release\uncompr.o obj\Release\zutil.o -lws2_32 -lIphlpapi
There should be -O3, -g and -DNO_BOOST in there.
You have most likely messed up your project settings. Try re-creating the project file but this time do not adjust the way the options are handled. If it still messes up then re-install codeblocks!
By default you get Release and Debug. Make sure the command line you are checking is the correct one. Are you setting the options on release and then going for a debug build?

Resources