#line 635 "/home/travis/build/felix-lang/felix/src/packages/flx_pkgconfig.fdoc"
  include "std/felix/flx_pkgconfig_core";
  include "std/felix/flx_pkgconfig_query_interface";
  
  class FlxPkgConfig_query
  {
    object FlxPkgConfigQuery (path:list[string]) implements FlxPkgConfigQuery_interface::FlxPkgConfigQuery_t =
    {
      var paths =
        match path with
        | #Empty => Empty[string]
        | Cons (h,t) =>
          let
            fun aux (lst:list[string]) (out:list[string]) =>
            match lst with
            | #Empty => rev out
            | Cons (h,t) => aux t (("--path+="+h)!out)
            endmatch
          in
          ("--path="+h) ! aux t Empty[string]
      ;
      match path with | #Empty => assert false; | _ => ; endmatch;
  
  
      method gen query (args:list[string]) =>
        FlxPkgConfig_core::flx_pkgconfig (paths + args)
      ;
  
      // Get all the values of a field in a particular package
      method gen getpkgfield (pkg:string, field:string) : list[string] = {
        var result,values = query$ list$ ("--field="+field, pkg);
        if result != 0 do
          println$ "Can't find package " + pkg;
          println$ "Searching in paths:";
          for path in paths do
            println$ "  " + path;
          done
          System::exit(1);
        done
        return values;
      }
  
      // Get the single value of a field in a particular package.
      // Bug out if missing or multiple values.
      method gen getpkgfield1 (pkg:string, field:string) : string = {
        var values = getpkgfield (pkg,field);
        match values with
        | Cons (h,#Empty) => return h;
        | #Empty =>
          println$ "Required field " + field + " not found in package "+pkg;
          System::exit(1);
        | _ =>
          println$ "Multiple values for field " + field + " in " + pkg + " not allowed, got" + str values;
          System::exit(1);
        endmatch;
      }
  
      // Get the single value of a field in a particular package.
      // Bug out if multiple values.
      method gen getpkgfieldopt (pkg:string, field:string) : opt[string] = {
        var values = getpkgfield (pkg,field);
        match values with
        | Cons (h,#Empty) => return Some h;
        | #Empty => return None[string];
        | _ =>
          println$ "Multiple values for field " + field + " in " + pkg + " not allowed, got" + str values;
          System::exit(1);
        endmatch;
      }
  
      method gen getpkgfielddflt (pkg:string, field:string) : string =>
        match getpkgfieldopt (pkg, field) with
        | Some h => h
        | #None => ""
        endmatch
      ;
  
      Get Requires closure.
      Result is topologically sorted with each package listed
      after ones it requires.
      method gen getclosure (pkg:string) : list[string] = {
        var result,values = FlxPkgConfig_core::flx_pkgconfig $ paths +
          "--keeprightmost" + "--rec" + "--list" +  pkg
        ;
        if result != 0 do
          println$ "missing package for closure of " + pkg;
          System::exit(1);
        done
        return rev values;
      }
    }
  }