Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 303035 - Gentoo Development guide/pkg_setup: could it be done with a case ?
Summary: Gentoo Development guide/pkg_setup: could it be done with a case ?
Status: RESOLVED OBSOLETE
Alias: None
Product: Documentation
Classification: Unclassified
Component: Devmanual (show other bugs)
Hardware: All Linux
: High trivial (vote)
Assignee: Gentoo Devmanual Team
URL: http://devmanual.gentoo.org/ebuild-wr...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-31 22:26 UTC by Felipe Balbi
Modified: 2013-02-22 23:54 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Felipe Balbi 2010-01-31 22:26:56 UTC
I believe the pkg_setup() sample in the development guide could be made better by using a case statement. I'm not a bash expert but I believe the patch below is working fine judging by the tests I made with a simpler version of the script.

By using a series of ifs, ebuilds could become difficult to maintain if the package supports several different (as the example proposes) GUIs. I suggest using a case statement as patches for another compilation flag would be really simple to review.

Hoping this will make gentoo documentation better, the proposed patch is available at "aditional information" below.


Reproducible: Always

Steps to Reproduce:
1. open the development guide
2. follow the links down to ebuild writing -> ebuild functions -> pkg_setup
3. read the sample

Actual Results:  
pkg_setup() {
    # We need to know which GUI we're building in several
    # different places, so work it out here.
    if use gtk ; then
        if use gtk2 ; then
            export mypkg_gui="gtk2"
        else
            export mypkg_gui="gtk1"
        fi
    elif use motif then
        export mypkg_gui="motif"
    else
        export mypkg_gui="athena"
    fi
}


Expected Results:  
something like the following:
 47 pkg_setup() {
 48     # We need to know which GUI we're building in several
 49     # different places, so work it out here.
 50     useing="$USE --"
 51     eval set -- $useing
 52     while true; do
 53         case $1 in
 54         gtk2)
 55             export mypkg_gui="gtk2";
 56             break;;
 57         gtk1)
 58             export mypkg_gui="gtk1";
 59             break;;
 60         motif)
 61             export mypkg_gui="motif"
 62             break;;
 63         athena)
 64             export mypkg_gui="athena";
 65             break;;
 66         --)
 67             break;;
 68         *)
 69             shift 1;;
 70         esac
 71     done
 72 }


patch for the file:

diff --git a/ebuild-writing/functions/pkg_setup/text.xml b/ebuild-writing/functi
index a7fe9d2..b8849a3 100644
--- a/ebuild-writing/functions/pkg_setup/text.xml
+++ b/ebuild-writing/functions/pkg_setup/text.xml
@@ -47,17 +47,28 @@ pkg_setup()
 pkg_setup() {
     # We need to know which GUI we're building in several
     # different places, so work it out here.
-    if use gtk ; then
-        if use gtk2 ; then
-            export mypkg_gui="gtk2"
-        else
-            export mypkg_gui="gtk1"
-        fi
-    elif use motif then
-        export mypkg_gui="motif"
-    else
-        export mypkg_gui="athena"
-    fi
+    useing="$USE --"
+    eval set -- $useing
+    while true; do
+        case $1 in
+        gtk2)
+            export mypkg_gui="gtk2";
+            break;;
+        gtk1)
+            export mypkg_gui="gtk1";
+            break;;
+        motif)
+            export mypkg_gui="motif"
+            break;;
+        athena)
+            export mypkg_gui="athena";
+            break;;
+        --)
+            break;;
+        *)
+            shift 1;;
+        esac
+    done
 }
 </codesample>
 </body>
Comment 1 Felipe Balbi 2010-01-31 23:11:09 UTC
ran more tests and the following also works. And looks better:

diff --git a/ebuild-writing/functions/pkg_setup/text.xml b/ebuild-writing/functi
index a7fe9d2..54c6318 100644
--- a/ebuild-writing/functions/pkg_setup/text.xml
+++ b/ebuild-writing/functions/pkg_setup/text.xml
@@ -47,17 +47,27 @@ pkg_setup()
 pkg_setup() {
     # We need to know which GUI we're building in several
     # different places, so work it out here.
-    if use gtk ; then
-        if use gtk2 ; then
-            export mypkg_gui="gtk2"
-        else
-            export mypkg_gui="gtk1"
-        fi
-    elif use motif then
-        export mypkg_gui="motif"
-    else
-        export mypkg_gui="athena"
-    fi
+    eval set -- "$USE --"
+    while true; do
+        case $1 in
+        gtk2)
+            export mypkg_gui="gtk2";
+            break;;
+        gtk1)
+            export mypkg_gui="gtk1";
+            break;;
+        motif)
+            export mypkg_gui="motif"
+            break;;
+        athena)
+            export mypkg_gui="athena";
+            break;;
+        --)
+            break;;
+        *)
+            shift 1;;
+        esac
+    done
 }
 </codesample>
 </body>
Comment 2 Jeremy Olexa (darkside) (RETIRED) archtester gentoo-dev Security 2010-01-31 23:47:40 UTC
The original code shows the usage of the use() function better than your proposed change (where the use() function is abstracted out). The use() function is used quite often and is beneficial to be exposed to, in my opinion.
Comment 3 Felipe Balbi 2010-02-01 00:28:27 UTC
(In reply to comment #2)
> The original code shows the usage of the use() function better than your
> proposed change (where the use() function is abstracted out). The use()
> function is used quite often and is beneficial to be exposed to, in my opinion.

ok, I'm new to gentoo so not really into its internals yet, does use() return the value of the USE variable ? If so, the case also applies. But judging from the original version use() is implemented so that it tests whether USE contains the argument we just passed, right ?

would there be a way to use "case" statements since it does, IMHO, make the code a lot easier to follow and extend ?

could use() be extended so that if we call it no arguments it returns the value of $USE ?
Comment 4 nm (RETIRED) gentoo-dev 2010-02-01 01:48:15 UTC
As stated on the front page of the devmanual:

"If you have any corrections, suggestions or improvements please file a bug at bugs.gentoo.org and assign it to qa@gentoo.org."

Not the GDP's problem. Reassigning to QA.
Comment 5 Ciaran McCreesh 2010-02-02 12:35:31 UTC
Part of the point of that code is that USE isn't in any particular order. The preference when multiple options are available is set by ebuilds, not by the user. Messing around with USE directly is generally a sign that you're doing something wrong.
Comment 6 Markos Chandras (RETIRED) gentoo-dev 2010-08-11 17:22:20 UTC
Doesn't the Code sample on 

http://devmanual.gentoo.org/ebuild-writing/functions/pkg_setup/index.html

works for you?
Comment 7 Felipe Balbi 2010-08-11 17:26:37 UTC
(In reply to comment #6)
> Doesn't the Code sample on 
> 
> http://devmanual.gentoo.org/ebuild-writing/functions/pkg_setup/index.html
> 
> works for you?

It does, I was only thinking about ways of improving it :-)
Comment 8 Markos Chandras (RETIRED) gentoo-dev 2013-02-22 23:54:24 UTC
Not sure if something is pending here. Reopen if needed