Skip to content

Commit

Permalink
alire_common.gpr: remove default host os to avoid mistakes on macOS (a…
Browse files Browse the repository at this point in the history
…lire-project#1298)

* alire_common.gpr: remove default host os to avoid mistakes on macOS

With the default being Linux, macOS users will not notice any issue until they try to use the executable.
Even then the issue will be obscure.

* Remove any implicit detection of the host OS

This should avoid misbuilds that mix Linux/macOS.

A bash function is now provided for detection of the OS in our scripts.

---------

Co-authored-by: Alejandro R. Mosteo <[email protected]>
  • Loading branch information
Fabien-Chouteau and mosteo authored Jan 31, 2023
1 parent 748b833 commit f9b34b5
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ on:
release:
types: [created]

env:
ALIRE_OS: "windows"

jobs:

build:
Expand Down
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ https://alire.ada.dev/

## TL;DR ##

Available for Linux/macOS/Windows.
Available for Linux/macOS/Windows/FreeBSD.

Download the latest stable version from the [Releases](https://github.com/alire-project/alire/releases) page. See the [Getting Started](doc/getting-started.md) guide for binary downloads.

If, instead, you want to test the latest development version, see [Building from sources](#building-from-sources).
If, instead, you want to test the latest development version, see [Building from sources](#building-from-sources) or, if you already have a recent `alr` in your system, [Building with `alr`](#building-with-alr).

## Installation and First Steps ##

See the [Getting Started](doc/getting-started.md) guide.

## Building from sources ##

The build process of `alr` is straighforward and depends only on a recent GNAT Ada 2012 compiler. All dependencies are included as submodules. A project file (`alr_env.gpr`) is provided to drive the build with all necessary configuration (see the macOS extra step below, though).
The build process of `alr` is straighforward and depends only on a recent GNAT Ada 2012 compiler. All dependencies are included as submodules. A project file (`alr_env.gpr`) is provided to drive the build with all necessary configuration (which is also valid for editing with GNAT Studio).

The ALIRE_OS environment variable must be set to the OS for which `alr` is being build, taking one of the values in `freebsd`, `linux`, `macos`, `windows`.

Follow these steps:

1. Clone the repository: `git clone --recurse-submodules https://github.com/alire-project/alire.git`
1. Enter the cloned repository folder.
1. Only on macOS: define the environment variable `OS=macOS`
1. Build the executable: `gprbuild -j0 -P alr_env`
1. Export the environment variable `ALIRE_OS=<one of: freebsd, linux, macos, windows>`
1. Build the executable: `gprbuild -j0 -p -P alr_env`

The binary will be found at `bin/alr`. You can run `alr version` to see version and diagnostics information.

Expand All @@ -49,7 +51,8 @@ Sourcing the `scripts/alr-completion.bash` file will provide bash tab autocomple

If you already have a recent enough `alr` binary, you can alternative build
`alr` by simply running `alr build` at the root of the repository. This command
will retrieve all necessary dependencies prior to launching the build.
will retrieve all necessary dependencies prior to launching the build and
configure the environment.

The master branch should normally be able to build itself in this fashion, as
this is one of our integration tests.
Expand All @@ -76,6 +79,8 @@ environment `alr` is using with `alr printenv`.

## Supported platforms ##

Alire can be built on Linux, macOS, Windows, and FreeBSD.

Alire requires a recent Ada 2012 compiler. In practice, this currently means
the latest [GNAT Community](https://www.adacore.com/download) or a somewhat
recent GNAT FSF. Continuous integration is run against the Windows and macOS
Expand Down
22 changes: 12 additions & 10 deletions alire.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,31 @@ library project Alire is

for Library_Name use "alire";

-- Set OS-dependent source files

Src_Dirs := ("src/alire");

case Alire_Common.Host_Os is
when "windows" => Src_Dirs := Src_Dirs & ("src/alire/os_windows");
when "osx" => Src_Dirs := Src_Dirs & ("src/alire/os_macos");
when "freebsd" => Src_Dirs := Src_Dirs & ("src/alire/os_freebsd");
when others => Src_Dirs := Src_Dirs & ("src/alire/os_linux");
when "linux" => Src_Dirs := Src_Dirs & ("src/alire/os_linux");
when "macos" => Src_Dirs := Src_Dirs & ("src/alire/os_macos");
when "windows" => Src_Dirs := Src_Dirs & ("src/alire/os_windows");
end case;

package Naming is
case Alire_Common.Host_OS is
when "windows" =>
for body ("Alire.Platforms.Current") use "alire-platforms-current__windows.adb";
for body ("Alire.Platforms.Folders") use "alire-platforms-folders__windows.adb";
when "osx" =>
for body ("Alire.Platforms.Current") use "alire-platforms-current__macos.adb";
for body ("Alire.Platforms.Folders") use "alire-platforms-folders__macos.adb";
when "freebsd" =>
for body ("Alire.Platforms.Current") use "alire-platforms-current__freebsd.adb";
for body ("Alire.Platforms.Folders") use "alire-platforms-folders__freebsd.adb";
when others =>
when "linux" =>
for body ("Alire.Platforms.Current") use "alire-platforms-current__linux.adb";
for body ("Alire.Platforms.Folders") use "alire-platforms-folders__linux.adb";
when "macos" =>
for body ("Alire.Platforms.Current") use "alire-platforms-current__macos.adb";
for body ("Alire.Platforms.Folders") use "alire-platforms-folders__macos.adb";
when "windows" =>
for body ("Alire.Platforms.Current") use "alire-platforms-current__windows.adb";
for body ("Alire.Platforms.Folders") use "alire-platforms-folders__windows.adb";
end case;
end Naming;

Expand Down
6 changes: 5 additions & 1 deletion alire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ CLIC_LIBRARY_TYPE="static"

# Building alr requires the explicit setting of this variable
[gpr-set-externals."case(os)"]
macos = { OS = "macOS" }
freebsd = { ALIRE_OS = "freebsd" }
linux = { ALIRE_OS = "linux" }
macos = { ALIRE_OS = "macos" }
windows = { ALIRE_OS = "windows" }
"..." = { ALIRE_OS = "unknown" } # Will cause the build to fail

# Some dependencies require precise versions during the development cycle:
[[pins]]
Expand Down
9 changes: 7 additions & 2 deletions alire_common.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ abstract project Alire_Common is

for Create_Missing_Dirs use "True";

Host_OS := external ("ALIRE_OS", "default");
-- Defined in alr_env.gpr.
type Host_OSes is ("linux",
"freebsd",
"macos",
"windows");

Host_OS : Host_OSes := external ("ALIRE_OS");
-- Set by user or in alire.toml

type Any_Build_Mode is ("debug", "release");
Build_Mode : Any_Build_Mode := external ("ALIRE_BUILD_MODE", "debug");
Expand Down
5 changes: 3 additions & 2 deletions alr.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ project Alr is
Src_Dirs := ("src/alr");

case Alire_Common.Host_Os is
when "freebsd" => Src_Dirs := Src_Dirs & ("src/alr/os_linux");
when "linux" => Src_Dirs := Src_Dirs & ("src/alr/os_linux");
when "macos" => Src_Dirs := Src_Dirs & ("src/alr/os_macos");
when "windows" => Src_Dirs := Src_Dirs & ("src/alr/os_windows");
when "osx" => Src_Dirs := Src_Dirs & ("src/alr/os_macos");
when others => Src_Dirs := Src_Dirs & ("src/alr/os_linux");
end case;

for Source_Dirs use Src_Dirs;
Expand Down
41 changes: 17 additions & 24 deletions alr_env.gpr
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
with "alire_common.gpr"; -- To gain access to Host_OS without redefining it

aggregate project Alr_Env is

-- To be able to build with this project file, the user must set the
-- host operating system in ALIRE_OS using one of the values defined
-- in alire_common.gpr

for Create_Missing_Dirs use "True";

for Project_Path use (
Expand All @@ -23,32 +29,19 @@ aggregate project Alr_Env is

for Project_Files use ("alr.gpr");

-- Provide defaults for environment variables that GNATcoll requires
-- Set environment variables for dependencies

case Alire_Common.Host_Os is
when "freebsd" => for External ("GNATCOLL_OS") use "unix";
when "linux" => for External ("GNATCOLL_OS") use "unix";
when "macos" => for External ("GNATCOLL_OS") use "osx";
when "windows" => for External ("GNATCOLL_OS") use "windows";
end case;

for External ("LIBRARY_TYPE") use "static";
for External ("BUILD") use "DEBUG";

Host_OS := external ("OS", "default");
-- On Windows an OS environment variable is defined, we can use it to
-- determine if we are compiling on Windows.
--
-- On macOS, the nearest equivalent is OSTYPE; however this is
-- e.g. "darwin18", so not useful here. Set "macOS" by hand.

-- ALIRE_OS is used in alire_common.gpr.
-- GNATCOLL_OS is used in gnatcoll.gpr.
case Host_OS is
when "Windows_NT" | "windows_nt" | "windows" | "Windows" =>
for External ("ALIRE_OS") use "windows";
for External ("GNATCOLL_OS") use "windows";
when "macOS" | "macos" | "OSX" | "osx" =>
for External ("ALIRE_OS") use "osx";
for External ("GNATCOLL_OS") use "osx";
when "freebsd" =>
for External ("ALIRE_OS") use "freebsd";
for External ("GNATCOLL_OS") use "unix";
when others =>
for External ("ALIRE_OS") use "unix";
for External ("GNATCOLL_OS") use "unix";
end case;
-- The user must set ALIRE_OS before building. See the list of valid values
-- in alire_common.gpr

end Alr_Env;
11 changes: 11 additions & 0 deletions dev/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Import reusable bits
pushd $( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) > /dev/null
. functions.sh
popd > /dev/null

export ALIRE_OS=$(get_OS)

echo Building with ALIRE_OS=$ALIRE_OS...
gprbuild -j0 -r -p -P `dirname $0`/../alr_env.gpr "$@"
3 changes: 0 additions & 3 deletions dev/devbuild.sh

This file was deleted.

51 changes: 51 additions & 0 deletions dev/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash

function guess_OS() {
# Returns one of the values needed in ALIRE_OS, using environment variables

if [ -n "$WINDIR" ]; then
echo windows
elif [ -n "$OSTYPE" ]; then
case "$OSTYPE" in # only available on bash shells
"linux-gnu")
echo linux
;;
"freebsd")
echo freebsd
;;
"darwin"*) # varies with versions: darwin18, darwin19, etc.
echo macos
;;
*)
echo unknown # give up, builds depending on ALIRE_OS will fail.
;;
esac
else
echo unknown
fi
}

function get_OS() {
# Returns one of the values needed in ALIRE_OS, using `uname``

OS=`uname -s`

case "$OS" in
"Linux")
echo linux
;;
"FreeBSD")
echo freebsd
;;
"Darwin")
echo macos
;;
"Windows")
echo windows
;;
*)
# Fall back to use environment clues
guess_OS
;;
esac
}
4 changes: 0 additions & 4 deletions dev/gpslaunch.sh

This file was deleted.

9 changes: 0 additions & 9 deletions dev/pull.sh

This file was deleted.

4 changes: 0 additions & 4 deletions dev/push.sh

This file was deleted.

7 changes: 7 additions & 0 deletions dev/unused.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#!/bin/bash

# Import reusable bits
pushd $( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) > /dev/null
. functions.sh
popd > /dev/null

export ALIRE_OS=$(get_OS)

gprclean -q -r -Palr_env

{
Expand Down
10 changes: 5 additions & 5 deletions scripts/ci-github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ set -o nounset

export PATH+=:${PWD}/bin

# For Darwin, have to define OS=macOS for alr_env.gpr
# Windows defines it anyway
# Linux (undefined) selects the default

[ `uname -s` == "Darwin" ] && export OS=macOS
# Import reusable bits
pushd $( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
. ../dev/functions.sh
popd

# Build alr
export ALIRE_OS=$(get_OS)
gprbuild -j0 -p -P alr_env

# Disable distro detection if supported
Expand Down

0 comments on commit f9b34b5

Please sign in to comment.