ICU-20924 Use pkg-config to generate the path to pkgdata.inc
diff --git a/icu4c/source/tools/pkgdata/pkgdata.cpp b/icu4c/source/tools/pkgdata/pkgdata.cpp
index 7235a7f..6406fcc 100644
--- a/icu4c/source/tools/pkgdata/pkgdata.cpp
+++ b/icu4c/source/tools/pkgdata/pkgdata.cpp
@@ -95,7 +95,7 @@
 static void createFileNames(UPKGOptions *o, const char mode, const char *version_major, const char *version, const char *libName, const UBool reverseExt, UBool noVersion);
 static int32_t initializePkgDataFlags(UPKGOptions *o);
 
-static int32_t pkg_getOptionsFromICUConfig(UBool verbose, UOption *option);
+static int32_t pkg_getPkgDataPath(UBool verbose, UOption *option);
 static int runCommand(const char* command, UBool specialHandling=FALSE);
 
 #define IN_COMMON_MODE(mode) (mode == 'a' || mode == 'c')
@@ -309,7 +309,7 @@
 
 #if !defined(WINDOWS_WITH_MSVC) || defined(USING_CYGWIN)
         if(!options[BLDOPT].doesOccur && uprv_strcmp(options[MODE].value, "common") != 0) {
-          if (pkg_getOptionsFromICUConfig(options[VERBOSE].doesOccur, &options[BLDOPT]) != 0) {
+          if (pkg_getPkgDataPath(options[VERBOSE].doesOccur, &options[BLDOPT]) != 0) {
                 fprintf(stderr, " required parameter is missing: -O is required for static and shared builds.\n");
                 fprintf(stderr, "Run '%s --help' for help.\n", progname);
                 return 1;
@@ -2158,41 +2158,46 @@
     } /* for each file list file */
 }
 
-/* Try calling icu-config directly to get the option file. */
- static int32_t pkg_getOptionsFromICUConfig(UBool verbose, UOption *option) {
+/* Helper for pkg_getPkgDataPath() */
 #if U_HAVE_POPEN
-    LocalPipeFilePointer p;
-    size_t n;
-    static char buf[512] = "";
+static UBool getPkgDataPath(const char *cmd, UBool verbose, char *buf, size_t items) {
     icu::CharString cmdBuf;
     UErrorCode status = U_ZERO_ERROR;
-    const char cmd[] = "icu-config --incpkgdatafile";
-    char dirBuf[1024] = "";
-    /* #1 try the same path where pkgdata was called from. */
-    findDirname(progname, dirBuf, UPRV_LENGTHOF(dirBuf), &status);
-    if(U_SUCCESS(status)) {
-      cmdBuf.append(dirBuf, status);
-      if (cmdBuf[0] != 0) {
-        cmdBuf.append( U_FILE_SEP_STRING, status );
-      }
-      cmdBuf.append( cmd, status );
-      
-      if(verbose) {
-        fprintf(stdout, "# Calling icu-config: %s\n", cmdBuf.data());
-      }
-      p.adoptInstead(popen(cmdBuf.data(), "r"));
+    LocalPipeFilePointer p;
+    size_t n;
+
+    cmdBuf.append(cmd, status);
+    if (verbose) {
+        fprintf(stdout, "# Calling: %s\n", cmdBuf.data());
+    }
+    p.adoptInstead( popen(cmdBuf.data(), "r") );
+
+    if (p.isNull() || (n = fread(buf, 1, items-1, p.getAlias())) <= 0) {
+        fprintf(stderr, "%s: Error calling '%s'\n", progname, cmd);
+        *buf = 0;
+        return FALSE;
     }
 
-    if(p.isNull() || (n = fread(buf, 1, UPRV_LENGTHOF(buf)-1, p.getAlias())) <= 0) {
-        if(verbose) {
-            fprintf(stdout, "# Calling icu-config: %s\n", cmd);
-        }
+    return TRUE;
+}
+#endif
 
-        p.adoptInstead(popen(cmd, "r"));
-        if(p.isNull() || (n = fread(buf, 1, UPRV_LENGTHOF(buf)-1, p.getAlias())) <= 0) {
-            fprintf(stderr, "%s: icu-config: No icu-config found. (fix PATH or use -O option)\n", progname);
+/* Get path to pkgdata.inc. Try pkg-config first, falling back to icu-config. */
+static int32_t pkg_getPkgDataPath(UBool verbose, UOption *option) {
+#if U_HAVE_POPEN
+    static char buf[512] = "";
+    UBool pkgconfigIsValid = TRUE;
+    const char *pkgconfigCmd = "pkg-config --variable=pkglibdir icu-uc";
+    const char *icuconfigCmd = "icu-config --incpkgdatafile";
+    const char *pkgdata = "pkgdata.inc";
+
+    if (!getPkgDataPath(pkgconfigCmd, verbose, buf, UPRV_LENGTHOF(buf))) {
+        if (!getPkgDataPath(icuconfigCmd, verbose, buf, UPRV_LENGTHOF(buf))) {
+            fprintf(stderr, "%s: icu-config not found. Fix PATH or specify -O option\n", progname);
             return -1;
         }
+
+        pkgconfigIsValid = FALSE;
     }
 
     for (int32_t length = strlen(buf) - 1; length >= 0; length--) {
@@ -2203,21 +2208,18 @@
         }
     }
 
-    if(buf[strlen(buf)-1]=='\n')
-    {
-        buf[strlen(buf)-1]=0;
-    }
-
-    if(buf[0] == 0)
-    {
-        fprintf(stderr, "%s: icu-config: invalid response from icu-config (fix PATH or use -O option)\n", progname);
+    if (!*buf) {
+        fprintf(stderr, "%s: Unable to locate pkgdata.inc. Unable to parse the results of '%s'. Check paths or use the -O option to specify the path to pkgdata.inc.\n", progname, pkgconfigIsValid ? pkgconfigCmd : icuconfigCmd);
         return -1;
     }
 
-    if(verbose) {
-      fprintf(stdout, "# icu-config said: %s\n", buf);
+    if (pkgconfigIsValid) {
+        uprv_strcat(buf, U_FILE_SEP_STRING);
+        uprv_strcat(buf, pkgdata);
     }
 
+    buf[strlen(buf)] = 0;
+
     option->value = buf;
     option->doesOccur = TRUE;