let select builddeparch profile dep = 
  let matcharch arch = Architecture.src_matches_arch arch builddeparch in
  match dep,profile with
  (* positive arch list, negative profiles, no profile selected
   *  -> only select if any of archs is in the arch list *)

  |(v,(((true,_)::_) as al),(((false,_)::_))),None when
    List.exists (fun (_,a) -> matcharch a) al -> Some v
  (* negative arch list, negative profiles, no profile selected
   *  -> only select if none of archs is in the arch list *)

  |(v,(((false,_)::_) as al),(((false,_)::_))),None when
    List.for_all (fun (_,a) -> not(matcharch a)) al -> Some v
  (* positive arch list, positive profiles, some profile selected
   *  -> only select if any of archs is in the arch list
   *     and the selected profile is in the profile list *)

  |(v,(((true,_)::_) as al),(((true,_)::_) as pl)),(Some p) when
    (List.exists (fun (_,a) -> matcharch a) al) &&
    (List.exists (fun (_,a) -> a = p) pl) -> Some v
  (* positive arch list, negative profiles, some profile selected
   *  -> only select if any of archs is in the arch list
   *     and the selected profile is not in the profile list *)

  |(v,(((true,_)::_) as al),(((false,_)::_) as pl)),(Some p) when
    (List.exists (fun (_,a) -> matcharch a) al) &&
    (List.for_all (fun (_,a) -> a <> p) pl) -> Some v
  (* negative arch list, positive profiles, some profile selected
   *  -> only select if none of archs is in the arch list
   *     and the selected profile is in the profile list *)

  |(v,(((false,_)::_) as al),(((true,_)::_) as pl)),(Some p) when
    (List.for_all (fun (_,a) -> not(matcharch a)) al) &&
    (List.exists (fun (_,a) -> a = p) pl) -> Some v
  (* negative arch list, negative profiles, some profile selected
   *  -> only select if none of archs is in the arch list
   *     and the selected profile is not in the profile list *)

  |(v,(((false,_)::_) as al),(((false,_)::_) as pl)),(Some p) when
    (List.for_all (fun (_,a) -> not(matcharch a)) al) &&
    (List.for_all (fun (_,a) -> a <> p) pl) -> Some v
  (* negative arch list, no profiles
   *  -> only select if none of archs is in the arch list *)

  |(v,(((false,_)::_) as al),[]),_ when
    List.for_all (fun (_,a) -> not(matcharch a)) al -> Some v
  (* positive arch list, no profiles
   *  -> only select if any of archs is in the arch list *)

  |(v,(((true,_)::_) as al),[]),_ when
    List.exists (fun (_,a) -> matcharch a) al -> Some v
  (* no arch list, negative profiles, some profile selected
   *  -> only select if the selected profile is not in the profile list *)

  |(v,[],(((false,_)::_) as pl)),(Some p) when
    List.for_all (fun (_,a) -> a <> p) pl -> Some v
  (* no arch list, positive profiles, some profile selected
   *  -> only select if the selected profile is in the profile list *)

  |(v,[],(((true,_)::_) as pl)),(Some p) when
    List.exists (fun (_,a) -> a = p) pl -> Some v
  (* no arch list, false profiles, no profile selected
   *  -> select *)

  |(v,[],(((false,_)::_))),None -> Some v
  (* no arch list, no profiles
   *  -> select *)

  |(v,[],[]),_ -> Some v
  (* any other case
   *  -> drop *)

  |-> None