From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 19D2DA0032;
	Mon, 13 Dec 2021 08:21:39 +0100 (CET)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 9B4CC40140;
	Mon, 13 Dec 2021 08:21:38 +0100 (CET)
Received: from mail-io1-f51.google.com (mail-io1-f51.google.com
 [209.85.166.51]) by mails.dpdk.org (Postfix) with ESMTP id 7B00A40042
 for <dev@dpdk.org>; Mon, 13 Dec 2021 08:21:36 +0100 (CET)
Received: by mail-io1-f51.google.com with SMTP id m9so17536280iop.0
 for <dev@dpdk.org>; Sun, 12 Dec 2021 23:21:36 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=mime-version:references:in-reply-to:from:date:message-id:subject:to
 :cc; bh=k8ST6BdgfGyezDtONReoIuZ3s1DzzWkfhHK9rjtuSUw=;
 b=jkZXXMFqE9dXeBRwGeRzZUll6U+w7EuL9MgOGvZUKlDVEv0TZQ1Nyg1fJDh0wGHuIa
 JSEYEhdbHwx3MYOAA97nn7FObUd+wVTdH10+x+CVHeDnraNcJPXuH4CoN1qcggnOJC0m
 sDHgxF5RwWNa6VB6vyeMm0KX5xhFee6Kh4S67DwyPOAfh1/1dL/nxWmu55R3N19Ad5pj
 0gIJVAg7MdwPq9oeG3zq1QwVcvY1u2H/mjdLXZ5cEKGfA9LTGPevm/plnVEdXFqyQrGS
 pilFaYnmIyzx9XSw3XJoY8ZAN0I42cwigWxsTM5/+rHju8ZBSqrK4iYA1Z6OoRDPuEPs
 MViA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:mime-version:references:in-reply-to:from:date
 :message-id:subject:to:cc;
 bh=k8ST6BdgfGyezDtONReoIuZ3s1DzzWkfhHK9rjtuSUw=;
 b=kjn7cJDEHol7k9MZn5+LszTA9LOXgC075fsk78JY2WT7wnXSLH9hnkPo8Iv3nHJSeZ
 ToBP5NuuNZ4pV9xgTkILZJLIvgxdxrFG4KLg6NFRIN3TOMCSP/z1ZZNUMHVwg2zh2+LO
 dkEEG8wGUciJ6trKBk4oswoBzLOXB5aZg5AhB/yXzcB3yAfeKSEzrPR1Q4ABWy7HKM31
 p8XGt5LUuNEQFeJCCq0As+m6N9AMDzRrwaoF4PzvPvZiICGx+fPz5OWwGmNhkgnzfkBd
 +qWlgGI7aWIi0lVBWvEbnEKO0op+TuSDIJfSYoDIpTrGjkbLIWMHuhQ3+YE4nlwd3dvY
 xWUQ==
X-Gm-Message-State: AOAM532swh8pEZFxBH6WuOBR/79ZSkNr4pjDpb0aF8LXEv0eELG/mDWG
 j0HUbajEOx/9JtgcrRZUrn4RnBXp5/xoov8oeX4=
X-Google-Smtp-Source: ABdhPJw0bUaubL5lbpAfIt8McPbUgURCjvL0MjcdWR5qXSEEe0Z0nBmG5MrKxlOHAyor135Y2YZAY5U5MBihGgViMEs=
X-Received: by 2002:a05:6638:224d:: with SMTP id
 m13mr31778827jas.86.1639380095770; 
 Sun, 12 Dec 2021 23:21:35 -0800 (PST)
MIME-Version: 1.0
References: <20211210145330.108256-1-bruce.richardson@intel.com>
In-Reply-To: <20211210145330.108256-1-bruce.richardson@intel.com>
From: Jerin Jacob <jerinjacobk@gmail.com>
Date: Mon, 13 Dec 2021 12:51:09 +0530
Message-ID: <CALBAE1Oo1Vmkd5DiX9GPVV5qLeHmoj0gfTMKoAyS_ctUtEFpTQ@mail.gmail.com>
Subject: Re: [PATCH] build/eal: add OS defines for C conditional checks
To: Bruce Richardson <bruce.richardson@intel.com>
Cc: dpdk-dev <dev@dpdk.org>, Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>, 
 Aaron Conole <aconole@redhat.com>
Content-Type: text/plain; charset="UTF-8"
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

On Fri, Dec 10, 2021 at 8:23 PM Bruce Richardson
<bruce.richardson@intel.com> wrote:
>
> Define a set of macros in the build configuration to allow C runtime
> code to check the current OS environment. This saves the user having to
> use ifdefs for e.g. disabling particular tests on Windows. See included
> documentation changes for usage examples.
>
> Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>

Acked-by: Jerin Jacob <jerinj@marvell.com>


> ---
>  doc/guides/contributing/coding_style.rst | 42 ++++++++++++++++++++++--
>  lib/eal/meson.build                      |  7 ++++
>  2 files changed, 47 insertions(+), 2 deletions(-)
>
> diff --git a/doc/guides/contributing/coding_style.rst b/doc/guides/contributing/coding_style.rst
> index 0ec37c019b..e52ecb2b60 100644
> --- a/doc/guides/contributing/coding_style.rst
> +++ b/doc/guides/contributing/coding_style.rst
> @@ -136,6 +136,12 @@ For example:
>  Conditional Compilation
>  ~~~~~~~~~~~~~~~~~~~~~~~
>
> +.. note::
> +
> + Conditional compilation should be used only when absolutely necessary, as it increases the number of target binaries that need to be built and tested.
> + See below for details of some utility macros/defines available to allow ifdefs/macros to be replaced by C conditional in some cases.
> +
> +
>  * When code is conditionally compiled using ``#ifdef`` or ``#if``, a comment may be added following the matching
>    ``#endif`` or ``#else`` to permit the reader to easily discern where conditionally compiled code regions end.
>  * This comment should be used only for (subjectively) long regions, regions greater than 20 lines, or where a series of nested ``#ifdef``'s may be confusing to the reader.
> @@ -165,9 +171,41 @@ Conditional Compilation
>   /* Or here. */
>   #endif /* !COMPAT_43 */
>
> -.. note::
> +Defines to Avoid Conditional Compilation
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +In many cases in DPDK, one wants to optionally compile code based on the target platform,
> +or runtime environment.
> +While this can be done using the conditional compilation directives,
> +e.g. ``#ifdef RTE_EXEC_ENV_LINUX``, present in DPDK for many releases,
> +this can also be done in many cases using regular ``if`` statements and the following defines:
> +
> +* ``RTE_ENV_FREEBSD``, ``RTE_ENV_LINUX``, ``RTE_ENV_WINDOWS`` - these define ids for each operating system environment.
> +* ``RTE_EXEC_ENV`` - this defines the id of the current environment, i.e. one of the items in list above.
> +* ``RTE_EXEC_ENV_IS_FREEBSD``, ``RTE_EXEC_ENV_IS_LINUX``, ``RTE_EXEC_ENV_IS_WINDOWS`` - 0/1 values indicating if the current environment is that specified,
> +  shortcuts for checking e.g. ``RTE_EXEC_ENV == RTE_ENV_WINDOWS``
> +
> +Examples of use:
> +
> +.. code-block:: c
> +
> +  /* report a unit tests as unsupported on Windows */
> +  if (RTE_EXEC_ENV_IS_WINDOWS)
> +     return TEST_SKIPPED;
> +
> +  /* set different default values depending on OS Environment */
> +  switch (RTE_EXEC_ENV) {
> +     case RTE_ENV_FREEBSD:
> +         default = x;
> +         break;
> +     case RTE_ENV_LINUX:
> +         default = y;
> +         break;
> +     case RTE_ENV_WINDOWS:
> +         default = z;
> +         break;
> +  }
>
> - Conditional compilation should be used only when absolutely necessary, as it increases the number of target binaries that need to be built and tested.
>
>  C Types
>  -------
> diff --git a/lib/eal/meson.build b/lib/eal/meson.build
> index 1722924f67..056beb9461 100644
> --- a/lib/eal/meson.build
> +++ b/lib/eal/meson.build
> @@ -10,6 +10,13 @@ if not is_windows
>      subdir('unix')
>  endif
>
> +exec_envs = {'freebsd': 0, 'linux': 1, 'windows': 2}
> +foreach env, id:exec_envs
> +    dpdk_conf.set('RTE_ENV_' + env.to_upper(), id)
> +    dpdk_conf.set10('RTE_EXEC_ENV_IS_' + env.to_upper(), (exec_env == env))
> +endforeach
> +dpdk_conf.set('RTE_EXEC_ENV', exec_envs[exec_env])
> +
>  dpdk_conf.set('RTE_EXEC_ENV_' + exec_env.to_upper(), 1)
>  subdir(exec_env)
>
> --
> 2.32.0
>