New test.
[mono.git] / mcs / tools / prj2make / pkgconfiginvoker.cs
1 // created on 6/8/2004 at 5:44 AM
2 using System;
3 using System.Diagnostics;
4
5 namespace Mfconsulting.General
6 {
7         public sealed class PkgConfigInvoker
8         {
9                 
10                 public static string GetPkgConfigVersion()
11                 {
12                         string pkgout = null;
13
14                         pkgout = RunPkgConfig("--version");
15
16                         if(pkgout != null)
17                         {
18                                 return pkgout;
19                         }
20
21                         return null;
22                 }
23
24                 public static string GetPkgVariableValue(string strPkg, string strVarName)
25                 {
26                         string pkgout = null;
27
28                         pkgout = RunPkgConfig(String.Format("--variable={0} {1}", 
29                                 strVarName, strPkg));
30
31                         if(pkgout != null)
32                         {
33                                 return pkgout;
34                         }
35
36                         return null;
37                 }
38
39                 public static string GetPkgConfigModuleVersion(string strPkg)
40                 {
41                         string pkgout = null;
42
43                         pkgout = RunPkgConfig(String.Format("--modversion {0}", strPkg));
44
45                         if(pkgout != null)
46                         {
47                                 return pkgout;
48                         }
49
50                         return null;
51                 }
52
53                 public static string RunPkgConfig(string strArgLine)
54                 {
55                         string pkgout;
56
57                         ProcessStartInfo pi = new ProcessStartInfo ();
58                         pi.FileName = "pkg-config";
59                         pi.RedirectStandardOutput = true;
60                         pi.UseShellExecute = false;
61                         pi.Arguments = strArgLine;
62                         Process p = null;
63                         try 
64                         {
65                                 p = Process.Start (pi);
66                         } 
67                         catch (Exception e) 
68                         {
69                                 Console.WriteLine("Couldn't run pkg-config: " + e.Message);
70                                 return null;
71                         }
72
73                         if (p.StandardOutput == null)
74                         {
75                                 Console.WriteLine("Specified package did not return any information");
76                         }
77                         
78                         pkgout = p.StandardOutput.ReadToEnd ();         
79                         p.WaitForExit ();
80                         if (p.ExitCode != 0) 
81                         {
82                                 Console.WriteLine("Error running pkg-config. Check the above output.");
83                                 return null;
84                         }
85
86                         if (pkgout != null)
87                         {
88                                 p.Close ();
89                                 return pkgout;
90                         }
91
92                         p.Close ();
93
94                         return null;
95                 }
96         }
97 }