From: Johannes 'josch' Schauer <josch@mister-muffin.de>
Date: Sun, 3 Jun 2018 10:47:27 +0200
Subject: Add support for --deb-drop-b-d-arch

Debian-bug: #850943
---
 applications/deb-buildcheck.ml  |  3 ++-
 applications/deb-coinstall.ml   |  4 ++--
 applications/distcheck.ml       |  2 +-
 deb/debcudf.ml                  |  2 ++
 deb/debcudf.mli                 |  1 +
 deb/sources.ml                  | 12 +++++++-----
 doc/manpages/ceve.pod           |  4 ++++
 doc/manpages/deb-buildcheck.pod |  4 ++++
 doseparse/stdLoaders.ml         |  7 ++++---
 doseparse/stdOptions.ml         |  6 ++++++
 10 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/applications/deb-buildcheck.ml b/applications/deb-buildcheck.ml
index 884e864..6eaf730 100644
--- a/applications/deb-buildcheck.ml
+++ b/applications/deb-buildcheck.ml
@@ -97,6 +97,7 @@ let main () =
   (* hostarch can be None *)
   let hostarch = match options.Debian.Debcudf.host with None -> "" | Some s -> s in
   let noindep = options.Debian.Debcudf.drop_bd_indep in
+  let noarch = options.Debian.Debcudf.drop_bd_arch in
   let dropalternatives = OptParse.Opt.get Options.dropalternatives in
   let profiles = options.Debian.Debcudf.profiles in
 
@@ -132,7 +133,7 @@ let main () =
               ~dropalternatives 
               ~profiles 
               ~filter:filter_external_sources 
-              ~noindep buildarch hostarch h 
+              ~noindep ~noarch buildarch hostarch h 
           in
           let pkglist = Deb.input_raw t in
           (pkglist,srclist)
diff --git a/applications/deb-coinstall.ml b/applications/deb-coinstall.ml
index b9775e4..c599d6a 100644
--- a/applications/deb-coinstall.ml
+++ b/applications/deb-coinstall.ml
@@ -57,7 +57,7 @@ module Options = struct
   let default =
     List.fold_left (fun acc e ->
       List.remove acc e
-    ) StdOptions.DistribOptions.default_options ["deb-host-arch";"deb-drop-b-d-indep";"deb-profiles"]
+    ) StdOptions.DistribOptions.default_options ["deb-host-arch";"deb-drop-b-d-indep";"deb-drop-b-d-arch";"deb-profiles"]
   in
   StdOptions.DistribOptions.add_debian_options ~default options ;
   StdOptions.DistribOptions.add_option options ~long_name:"deb-tupletable"
@@ -121,7 +121,7 @@ let main () =
     in
     let srclist = 
       if not(Option.is_none sources) then
-        Sources.sources2packages ~noindep:true ~profiles:[] native native origsourcelist
+        Sources.sources2packages ~noindep:true ~noarch:false ~profiles:[] native native origsourcelist
       else []
     in
     let tables = Debian.Debcudf.init_tables ~options (srclist@pkglist) in
diff --git a/applications/distcheck.ml b/applications/distcheck.ml
index 388f4e3..5d30c4d 100644
--- a/applications/distcheck.ml
+++ b/applications/distcheck.ml
@@ -49,7 +49,7 @@ module Options = struct
     List.fold_left
       List.remove
       StdOptions.DistribOptions.default_options
-      ["deb-host-arch";"deb-drop-b-d-indep";"deb-profiles"]
+      ["deb-host-arch";"deb-drop-b-d-indep";"deb-drop-b-d-arch";"deb-profiles"]
   in
   StdOptions.DistribOptions.add_debian_options ~default options ;
   StdOptions.DistribOptions.add_opam_options ~default options ;;
diff --git a/deb/debcudf.ml b/deb/debcudf.ml
index 8c83f0d..44f72e8 100644
--- a/deb/debcudf.ml
+++ b/deb/debcudf.ml
@@ -65,6 +65,7 @@ type options = {
   ignore_essential : bool;
   builds_from : bool;
   drop_bd_indep : bool;
+  drop_bd_arch : bool;
   profiles : string list; (* list of active build profiles *)
 }
 
@@ -76,6 +77,7 @@ let default_options = {
   ignore_essential = false;
   builds_from = false;
   drop_bd_indep = false;
+  drop_bd_arch = false;
   profiles = [];
 }
 
diff --git a/deb/debcudf.mli b/deb/debcudf.mli
index 76c9eb6..be90947 100644
--- a/deb/debcudf.mli
+++ b/deb/debcudf.mli
@@ -24,6 +24,7 @@ type options = {
   ignore_essential : bool ;
   builds_from : bool ;    (** whether to add the builds-from relationship from binary to source packages *)
   drop_bd_indep : bool ;  (** whether or not to ignore the Build-Depends-Indep field *)
+  drop_bd_arch : bool ;   (** whether or not to ignore the Build-Depends-Arch field *)
   profiles : string list ; (** list of active build profiles *)
 }
 
diff --git a/deb/sources.ml b/deb/sources.ml
index c14bfb5..ddf0d2f 100644
--- a/deb/sources.ml
+++ b/deb/sources.ml
@@ -169,7 +169,7 @@ let select hostarch profiles (v,al,pl) =
 (* XXX src2pkg could be skip using the same trick we use in opam, 
  * where dependencies are giltered at parsing time *)
 (* the package name is encodes as src:<package-name> *)
-let src2pkg ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(src="src") buildarch hostarch srcpkg =
+let src2pkg ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(noarch=false) ?(src="src") buildarch hostarch srcpkg =
   let conflicts l = List.filter_map (select hostarch profiles) l in
   (* imitate sbuild behaviour and drop all alternatives except those that have
    * the same name as the first. Search for RESOLVE_ALTERNATIVES in
@@ -189,6 +189,8 @@ let src2pkg ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(src="src
   let extras_profiles  = match profiles with [] -> [] | _ -> [("profiles", String.join " " profiles)] in
   let depends_indep   = if noindep then [] else srcpkg#build_depends_indep in
   let conflicts_indep = if noindep then [] else srcpkg#build_conflicts_indep in
+  let depends_arch   = if noarch then [] else srcpkg#build_depends_arch in
+  let conflicts_arch = if noarch then [] else srcpkg#build_conflicts_arch in
   (* when crossbuilding (host != build), implicitly depend on build-essential
    * and crossbuild-essential-$hostarch. When compiling natively, implicitly
    * depend on build-essential *)
@@ -200,8 +202,8 @@ let src2pkg ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(src="src
   new Packages.package ~name:("",Some(src ^ sep ^ srcpkg#name)) ~version:("",Some(srcpkg#version))
   ~architecture:("",Some(String.concat "," srcpkg#architecture))
   ~source:("",Some (srcpkg#name, Some srcpkg#version)) 
-  ~depends:("",Some (build_essential @ (depends (depends_indep @ srcpkg#build_depends @ srcpkg#build_depends_arch))))
-  ~conflicts:("",Some (conflicts (conflicts_indep @ srcpkg#build_conflicts @ srcpkg#build_conflicts_arch)))
+  ~depends:("",Some (build_essential @ (depends (depends_indep @ srcpkg#build_depends @ depends_arch))))
+  ~conflicts:("",Some (conflicts (conflicts_indep @ srcpkg#build_conflicts @ conflicts_arch)))
   ~extras:([],Some(extras_profiles @ [("Type",src)])) []
 ;;
 
@@ -209,8 +211,8 @@ let src2pkg ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(src="src
  * decide whether to select or drop that dependency *)
 (** transform a list of sources packages into dummy binary packages.
   * This function preserve the order *)
-let sources2packages ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(src="src") buildarch hostarch =
-  List.map (src2pkg ~dropalternatives ~profiles ~noindep ~src buildarch hostarch)
+let sources2packages ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(noarch=false) ?(src="src") buildarch hostarch =
+  List.map (src2pkg ~dropalternatives ~profiles ~noindep ~noarch ~src buildarch hostarch)
 ;;
 
 (** Check if a package is of "Type" source as encoded by the function sources2packages *)
diff --git a/doc/manpages/ceve.pod b/doc/manpages/ceve.pod
index c1bd2c7..82e98fd 100644
--- a/doc/manpages/ceve.pod
+++ b/doc/manpages/ceve.pod
@@ -304,6 +304,10 @@ Comma separated list of activated build profiles.
 
 Drop Build-Depends-Indep dependencies from Debian source packages.
 
+=item B<--deb-drop-b-d-arch>
+
+Drop Build-Depends-Arch dependencies from Debian source packages.
+
 =back
 
 =head1 EXAMPLES
diff --git a/doc/manpages/deb-buildcheck.pod b/doc/manpages/deb-buildcheck.pod
index 6e2e773..8426013 100644
--- a/doc/manpages/deb-buildcheck.pod
+++ b/doc/manpages/deb-buildcheck.pod
@@ -216,6 +216,10 @@ Convert Arch:all packages to Multi-Arch: foreign
 
 Drop Build-Indep dependencies
 
+=item B<--deb-drop-b-d-arch>
+
+Drop Build-Arch dependencies
+
 =item B<--deb-include-extra-source>
 
 Include packages with Extra-Source-Only:yes (dropped by default)
diff --git a/doseparse/stdLoaders.ml b/doseparse/stdLoaders.ml
index 94be9c0..afa9c5c 100644
--- a/doseparse/stdLoaders.ml
+++ b/doseparse/stdLoaders.ml
@@ -49,6 +49,7 @@ let read_deb ?filter ?(extras=[]) fname =
 let deb_load_list options ?(status=[]) ?(raw=false) dll =
   Util.Timer.start deb_load_list_timer;
   let noindep = options.Debian.Debcudf.drop_bd_indep in
+  let noarch = options.Debian.Debcudf.drop_bd_arch in
   let profiles = options.Debian.Debcudf.profiles in
   let pkgll = List.map (List.map (function
       | Deb p -> p
@@ -57,7 +58,7 @@ let deb_load_list options ?(status=[]) ?(raw=false) dll =
             fatal "--deb-native-arch was not specified while treating Debian Sources File";
         let buildarch = Option.get options.Debian.Debcudf.native in
         let hostarch = Option.get options.Debian.Debcudf.host in
-        Debian.Sources.src2pkg ~noindep ~profiles buildarch hostarch p
+        Debian.Sources.src2pkg ~noindep ~noarch ~profiles buildarch hostarch p
       | _ -> fatal "cannot handle input"
     )) dll 
   in
@@ -444,10 +445,10 @@ let supported_formats () =
 ;;
 
 (** return a list of Debian packages from a debian source file *)
-let deb_load_source ?filter ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) buildarch hostarch sourcefile =
+let deb_load_source ?filter ?(dropalternatives=false) ?(profiles=[]) ?(noindep=false) ?(noarch=false) buildarch hostarch sourcefile =
   Util.Timer.start deb_load_source_timer;
   let l = Debian.Sources.input_raw ?filter ~archs:[hostarch] [sourcefile] in
-  let r = Debian.Sources.sources2packages ~dropalternatives ~noindep ~profiles buildarch hostarch l in
+  let r = Debian.Sources.sources2packages ~dropalternatives ~noindep ~noarch ~profiles buildarch hostarch l in
   Util.Timer.stop deb_load_source_timer r
 ;;
 
diff --git a/doseparse/stdOptions.ml b/doseparse/stdOptions.ml
index 19d4ab2..f2d8201 100644
--- a/doseparse/stdOptions.ml
+++ b/doseparse/stdOptions.ml
@@ -320,6 +320,7 @@ module DistribOptions = struct
   let deb_ignore_essential = StdOpt.store_true ()
   let deb_builds_from = StdOpt.store_true ()
   let deb_drop_bd_indep = StdOpt.store_true ()
+  let deb_drop_bd_arch = StdOpt.store_true ()
   let deb_profiles = str_list_option ()
 
   let opam_switch = StdOpt.str_option ~default:"system" ()
@@ -333,6 +334,7 @@ module DistribOptions = struct
     "deb-ignore-essential";
     "deb-builds-from";
     "deb-drop-b-d-indep";
+    "deb-drop-b-d-arch";
     "deb-profiles";
     "opam-switch";
     "opam-switches";
@@ -380,6 +382,7 @@ module DistribOptions = struct
       ignore_essential = Opt.get deb_ignore_essential;
       builds_from = Opt.get deb_builds_from;
       drop_bd_indep = Opt.get deb_drop_bd_indep;
+      drop_bd_arch = Opt.get deb_drop_bd_arch;
       profiles = profiles;
     }
   ;;
@@ -470,6 +473,9 @@ module DistribOptions = struct
       if List.mem "deb-drop-b-d-indep" default then
         add options ~group ~long_name:"deb-drop-b-d-indep"
           ~help:"Drop the Build-Depends-Indep field from source packages (build no Architecture:all packages)" deb_drop_bd_indep;
+      if List.mem "deb-drop-b-d-arch" default then
+        add options ~group ~long_name:"deb-drop-b-d-arch"
+          ~help:"Drop the Build-Depends-Arch field from source packages (build no Architecture:any packages)" deb_drop_bd_arch;
       if List.mem "deb-profiles" default then
         add options ~group ~long_name:"deb-profiles"
           ~help:"comma separated list of activated build profiles" deb_profiles;
