What are the common platform names in Buck? - c++

I would like to build a cross-platform cxx_library with buck. I have different cpp files for the different platforms. According to the docs, I can handle this using platform_srcs, which is:
...a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched".
What do these platform names look like?
What are some example regexes I could use for OSX, Windows and Linux?

Android: android
iPhone: iphoneos
iPhone Simulator: iphonesimulator
Linux: Unsure
OSX: Unsure
Windows: ^windows.*

I am using the following:
Android: android.*
iPhone: iphoneos.*
iPhone Simulator: iphonesimulator*
Linux: linux.*
macOS: macos.*
Windows: windows.*
Don't forget to turn on should_remap_host_platform in order to avoid 'default' platform weirdness. In your .buckconfig:
[cxx]
should_remap_host_platform = true
See: https://github.com/facebook/buck/issues/2073

Related

Build ICU for iOS

I need ICU library for iPhone. I have tried to build it from souce, however, I am getting this erros:
clang++ ... /Users/petr/Development/icu-cross-compile-master/icu-60-2/source/tools/pkgdata/pkgdata.cpp
/Users/petr/Development/icu-cross-compile-master/icu-60-2/source/tools/pkgdata/pkgdata.cpp:544:18: error: call to unavailable function 'system': not available on iOS
int result = system(cmd);
^~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS11.2.sdk/usr/include/stdlib.h:195:6: note: candidate function has been explicitly made unavailable
int system(const char *) __DARWIN_ALIAS_C(system);
sh ${ICU_SOURCE}/configure --host=arm-apple-darwin --with-cross-build=${PREBUILD_DIR} ${PREFIX}
My PREFIX configuration is as:
--enable-extras=yes
--enable-tools=yes
--enable-icuio=yes
--enable-strict=no
--enable-static
--enable-shared=no
--enable-tests=yes
--disable-renaming
--enable-samples=no
--enable-dyload=no
--with-data-packaging=static
Or is there any other way, how to generate libicudata.a? The similar build script works OK for Android, Mac and Win. Only iPhone is problem.
The problem is that system() is deprecated for iOS 11.
I think quick fix for you will be in using Xcode 6, instead of Xcode 9, so you can compile for iOS 7 as a target, where system() wasn't deprecated.
Or, if you really need iOS full compatible solution, you need to re-write ICU source code to use posix spawn functionality instead of system(). Check this answer for more details: How do you use posix_spawn to replace the deprecated 'system' to launch opendiff in Objective-C?
Another solution to successfully build ICU on iOS as a library is to build without tools with the configure flag --build-tools=no. The following is the complete set of configure flags I'm using to build for various platforms, android and ICU included.
--enable-static=yes
--enable-shared=no
--enable-extras=no
--enable-strict=no
--enable-icuio=no
--enable-layout=no
--enable-layoutex=no
--enable-tests=no
--enable-samples=no
--enable-tools=no
--enable-dyload=no
--with-data-packaging=archive
I've just made a makefile which will download the ICU source and create a universal framework for Mac, Catalyst, iOS Simulator and iOS. This can then just be drag-and-dropped into your project.
https://github.com/dbquarrel/icu4c-xcframework
Might help you (though many years late).
My use was to enable the icu tokenizer in sqlite for iOS.

How to compile CodeBlocks MingW in Windows to Ubuntu or Centos

Is there a way to compile with MingW with CodeBlocks in Windows so they can be used in Ubuntu or Centos distros?
I've tried compiling with GNU GCC option then got the output file with .o extensions under obj/Release/ folder.
When I run I get this error under my Vagrant Ubuntu machine:
-bash: ./main.o: cannot execute binary file
How can I compile it so it runs on my Linux machines?
The technical term for what you're trying to accomplish is cross-compilation. For that, you need to build a specific cross-compiler using GCC sources. If you still want to keep MinGW, there is a page explaining the steps needed to create a ARM cross-compiler : http://www.mingw.org/wiki/HostedCrossCompilerHOWTO. (you'll have to modify the target)
List of targets supported by GCC :
armv5te-android-gcc armv5te-linux-rvct armv5te-linux-gcc
armv5te-none-rvct
armv6-darwin-gcc armv6-linux-rvct armv6-linux-gcc
armv6-none-rvct
armv7-android-gcc armv7-darwin-gcc armv7-linux-rvct
armv7-linux-gcc armv7-none-rvct
mips32-linux-gcc
ppc32-darwin8-gcc ppc32-darwin9-gcc ppc32-linux-gcc
ppc64-darwin8-gcc ppc64-darwin9-gcc ppc64-linux-gcc
sparc-solaris-gcc
x86-android-gcc x86-darwin8-gcc x86-darwin8-icc
x86-darwin9-gcc x86-darwin9-icc x86-darwin10-gcc
x86-darwin11-gcc x86-darwin12-gcc x86-linux-gcc
x86-linux-icc x86-os2-gcc x86-solaris-gcc
x86-win32-gcc x86-win32-vs7 x86-win32-vs8
x86-win32-vs9
x86_64-darwin9-gcc x86_64-darwin10-gcc x86_64-darwin11-gcc
x86_64-darwin12-gcc x86_64-linux-gcc x86_64-linux-icc
x86_64-solaris-gcc x86_64-win64-gcc x86_64-win64-vs8
x86_64-win64-vs9
universal-darwin8-gcc universal-darwin9-gcc universal-darwin10-gcc
universal-darwin11-gcc universal-darwin12-gcc
generic-gnu
There is only one big caveat : since Windows is not POSIX compliant, I don't think you can use signals or pthreads.
Finally, brace yourself because it's a tedious task to build a cx-compiler (lots of obscure bugs). That's why profesionnal devs pays $$$ for "plug'n'play" solutions.
EDIT : this MXE project can be useful to you

How to disable Eclipse CDT code formatter for a code block

The CDT code formatter has a pretty decent selection of options, but it doesn't seem to have to a feature that allows one to tell it to ignore a block of code. This feature exists in the Java code formatter:
// #formatter:off
... // code that should not be formatted
// #formatter:on
Does this feature exist and I just don't know about it, or does anyone know of any decent work-arounds?
In my particular case, I'm trying to define data structures (enum types and arrays of strings) that I want to have specific layouts.
Use Astyle (Artistic Style) formatter, it's far superior to the Eclipse CDT built-in formatter and has the feature you require:
http://astyle.sourceforge.net/astyle.html#_Disable_Formatting
Example:
#include <iostream>
int main(int argc, char** argv)
{
// *INDENT-OFF*
std::cout<<"hello world"<<'\n';
// *INDENT-ON*
}
Formatting this using astyle won't indent the code between // INDENT-OFF and // INDENT-ON but it will also disable any other formatting features astyle does, like the spacing of the instructions in this case.
I use it myself configured as an external tool.
The only problem, external tools don't have hotkeys, but there is one hotkey to "Run Last Launched External Tool", and if you only use one external tool it works the same.
More details about the configuration (linux):
Astyle:
You can get it easily from your distribution repositories or via the official site.
To setup a configuration file with the formatting settings:
http://astyle.sourceforge.net/astyle.html#_Options_File
I use the home folder variant, just create a .astylerc in your $HOME, mine contains:
--suffix=none
--style=allman
--indent=tab=4
--max-code-length=70
--close-templates
--keep-one-line-blocks
--break-elseifs
--break-closing-brackets
--align-reference=type
--align-pointer=type
--indent-classes
--indent-modifiers
--indent-switches
--indent-cases
--indent-labels
--indent-col1-comments
--min-conditional-indent=0
--pad-oper
--pad-header
--unpad-paren
Eclipse:
"Run" menu --> External tools --> External tools Configurations... Add a new "Program" and in the configuration window:
Location: /usr/bin/astyle (use whereis or locate to check this)
Working Directory: ${project_loc}
Arguments: ${selected_resource_loc}
In the same window, refresh tab:
Tick Refresh resources upon completion.
Tick "The selected resource"
Same window, common tab:
Display in favorites menu, Tick "External tools"
Yes, you can do it since CDT supports this feature starting from version 9.7. The behavior is exactly the same of JDT.
If you are using OS X or Linux (I haven't checked Windows, but it may be supported), you can use clang-format and CppStyle instead.
clang-format is a formatter utility which is provided with Clang, and it supports on/off comments // clang-format on and // clang-format off in C/C++/ObjC code. An introduction to build Clang and its utility tools can be found here.
http://clang.llvm.org/get_started.html
You do not need to install whole Clang and LLVM files on your system. Because clang-format is a standalone program which works without Clang. The on/off comments are not supported in old versions, so please use ver 3.7 (available from SVN as of Feb 2015).
CppStyle is an Eclipse plugin which enables us to use clang-format from Eclipse CDT.
https://github.com/wangzw/cppstyle
FYI. Here is the same feature request in the CDT Bugzilla. The functionality might be officially supported in future, but using clang-format or Astyle seems to be a better solution at the moment. https://bugs.eclipse.org/bugs/show_bug.cgi?id=453926
I guess I could stick these in a file with an extension ignored by the formatter and include this file where appropriate. I tried this out and it works - the data structure gets picked up the indexer (i.e. autocomplete works). Still, it would be nice to have an equivalent to the Java "#formatter:..." syntax.
As far as I know the answer is simply no, such a feature does not exist. You might be able to implement such a feature using the SDK though. Beware that in my experience the documentation is very incomplete and it's very hard to find an Eclipse developer who would be willing to help you fill in the holes. But since the feature exists in the Java formatter and it is an open source product, perhaps you could port over the logic to the C++ formatter.
You could also avoid formatting the whole file, and instead format only by selection.

how I get version of a program in linux

In Windows you can do:
CSystemInfo info;
this->m_strVersion = info.GetFileVersion( CFileSystemHelper::GetApplicationPath() + _T("/test.exe") );
to get the version number.
How would I do it in C++ on linux ?
Windows adopts a version resource system with standard API support, Linux and UNIX have no such high level concepts for a variety of reasons ranging from legacy to redundancy.
Best options are to query the local packaging system (RPM, APT, etc), or try executing with --version command line parameter which is a recommended GNU standard.
Example RPM query on command line for the Samba tool smbget:
# rpm -q -f /usr/bin/smbget --queryformat '%{version}\n'
3.0.33
You probably want to retrieve the path of the currently executing executable.
On Linux, you could use the /proc/ pseudo-file system. Read the proc(5) man page for more.
Specifically, you probably want to do something like
char myexepath[512];
memset (myexepath, 0, sizeof(myexepath);
readlink ("/proc/self/exe", myexepath, sizeof(myexepath));
(but you really should check for runtime errors above)
If you simply wanted to display the version of a program, you should have a convention about it. Usually accepting --version as the program first argument.
I invite you to read Advanced Linux Programming.

in the code (.c file) how I can find the linux distribution name version

I'd like to know in the code (.c file) how I can find the linux distribution name version (like ubuntu 10.0.4, or centOS 5.5...)?
The c function that I'm looking for should be like the uname() system call used in (.c files) to get kernel version.
it will be appreciated that the function is working for all linux distribution (standard)
I 'm not looking to get distribution name and version by the use of command line linux from code (.c file) (like the use of system("cat /etc/release");).
Any suggestion will be appreciated!
Regards
There is no standard for this yet. You can query following files or check for existence:
/etc/lsb-release
/etc/issue
/etc/*release
/etc/*version
Well, you can (and should) use fopen and fgets instead of system("cat"), for reading /etc/release.
There's no universal method though, I can even build a linux image which has no filesystem at all (except initramfs) and definitely no distribution name.
AFAIK there isn't a standard system call to get this if uname(2) doesn't give you enough info.
Safest approach is probably to check for "/proc/version" and read that
You could fopen("/etc/lsb-release") and parse its contents. It looks like this:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.3 LTS"
This method is not universal. You'll need to make sure that it works on all distros that you care about (if it doesn't, I suggest you go with #ott--'s answer).
Is it acceptable to run some shell commands?
$ /usr/bin/lsb_release -r
Release: 11.04
$ /usr/bin/lsb_release -d
Description: Ubuntu 11.04
$ /usr/bin/lsb_release -rd
Description: Ubuntu 11.04
Release: 11.04
There is no portable way to do that, you'll have to use some OS detection tool/library.
Fortunately, there are a few out there. I know those 2 :
Facter, a professional (yet free/open) information gathering program in ruby : http://puppetlabs.com/puppet/related-projects/facter/
a shell script : http://www.novell.com/coolsolutions/feature/11251.html
(I used facter via puppet and it is very good.)
With a little additional scripting, you can use one of those program's output to generate a .h that you can then use in your code.
You can even integrate this generation as a step in your makefile.
I usually inspect /etc/issue; while (as others pointed out) it is not guaranteed, I've fount in the field that's quite reliable.
As far as I've experienced, it works on ubuntu, debian, redhat, centos, slackware and archlinux.