Mailinglist Archive: opensuse-buildservice (153 mails)

< Previous Next >
Re: [opensuse-buildservice] Build C++ Files for different distributions
  • From: Brüns, Stefan <Stefan.Bruens@xxxxxxxxxxxxxx>
  • Date: Fri, 7 Nov 2014 18:09:30 +0000
  • Message-id: <1744220.G7yEoXGzKj@sbruens-linux>
Hi Tobias,

building packages for different Distributions and Architectures is a task the
openSUSE build service (OBS) comes in very handy.

Currently it supports all major distributions, enterprise and community, and
most major architectures (MIPS being the only missing one currently).

That said, although OBS leverages you from several tasks, most importantly
recurring ones, there is still some initial setup you have to do for your
software.

You said you will be using C++. So lets see how to get from your source code
to the installable package:

1. You need a build environment. For the beginning, just install a C++
compiler, e.g. GCC.

2. Next, you have to create a "recipe" how to create a binary from the source.

2.1 If you have just a single source file and are just needing the c++
standard library (libstdc++), all you need is calling:
$> g++ -o foo foo.cpp
The linking to libstdc++ is implicit, and this creates an "foo" executable.
Typical "Hello World!" example.

2.2. Things will become more complicated soon, as you have several source
files, start linking to third party libraries and so on ..
Now you have to start thinking about automting these steps. Typically, this is
done using a Makefile, which stores the recipes, which is executed by GNU
make.
As writing Makefiles by hand becomes error prone very soon, often a Makefile
generator is used. Prominent examples are GNU automake and CMake. I prefer
CMake, as writing rules for it is (IMHO) much more straightforward than
automake.

Assuming you now have some source files and an (autogenerated) makefile.
Everytime you execute "make", the sources are recompiled (as needed) and the
executable is created. Create an archive from your sources. Assuming you have
created an directory "foo" for your sources:
$> cd ..
$> tar caf foo-0.1.tar.xz foo

---

Up until now you have executed the building on your own computer, building an
executable for the distribution and architecture you are using. Next, you want
to replicate the steps done so far (installing the distribution, installing
the build environment, executing the build) for some other target.

For the beginning, you should chose just one target and adapt as needed when
adding more targets.

As I am only familiar with RPM based distributions, the next steps are just
given for openSUSE, which is RPM based, but this is just a limitation of
myself, not the OBS.

3. Start using the OBS. Create an account, add a new Subproject (e.g. "Bar")
to your home Project, and create an empty Package ("Foo") in it. You should
also add some base repository to your Project, e.g. openSUSE_13.1 or
openSUSE_13.2.

Next, you should replicate the current state of the package from the OBS to
your local computer.
$> osc checkout home:<accountName>:Bar Foo
This will create a new directory home:<accountName>:Bar with a subdirectory
Foo. Change to this directory. It will be almost empty.

Next steps ...

4. Creating the recipe for installing the distribution/build environment and
doing the build - aka creating a "specfile"

4.1. Use
https://build.opensuse.org/package/view_file/openSUSE:13.2/vim/spec.skeleton?expand=1
as a specfile template. Rename it to foo.spec

4.2. Fill out the missing Key:'s. Have a look at some other specfiles, and
read
https://en.opensuse.org/openSUSE:Specfile_guidelines#Preamble

The most important keys are Source: and BuildRequires: The Source should be an
archive of your sources, e.g. foo-0.1.tar.xz. BuildRequires is at least "g++",
but maybe also make and cmake.

4.3. After you have created the specfile foo.spec and copied the source
archive to the same directory, you can test things locally:
$> osc add foo-0.1.tar.xz
$> osc add foo.spec
$> osc build

Now you will most likely get some errors. Fix these, use google, and if you
can't find anything helpful (unlikely), feel free to ask here again. Reexecute
"osc build", as you fix errors.

After you have fixed all errors, submit your files to the OBS.
$> osc checkin

Now you can watch OBS building your package. Add more repositories. For simple
packages, e.g. other openSUSE versions and most likely Fedora will work
without further changes. Unfortunately, Debian/Ubuntu and Arch will require
more work.

Kind regards,

Stefan


--
Stefan Brüns / Bergstraße 21 / 52062 Aachen
home: +49 241 53809034 mobile: +49 151 50412019
work: +49 2405 49936-424--
To unsubscribe, e-mail: opensuse-buildservice+unsubscribe@xxxxxxxxxxxx
To contact the owner, e-mail: opensuse-buildservice+owner@xxxxxxxxxxxx

< Previous Next >
References