Use an environment variable for the config file.

This allows removing the gflags dependency from the config package.

Bug: 114301828
Change-Id: I5a0d4a094dac77862becc713a5ec3222942100e0
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index aef2f25..0218698 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -267,8 +267,6 @@
       "cuttlefish_config.json");
 }
 
-std::string GetConfigFileArg() { return "-config_file=" + GetConfigFile(); }
-
 std::string GetGuestPortArg() {
   constexpr int kEmulatorPort = 5555;
   return std::string{"--guest_ports="} + std::to_string(kEmulatorPort);
@@ -335,8 +333,7 @@
   }
 
   cvd::subprocess({FLAGS_virtual_usb_manager_binary,
-                   "-usb_v1_fd=" + std::to_string(server_fd),
-                   GetConfigFileArg()});
+                   "-usb_v1_fd=" + std::to_string(server_fd)});
 
   close(server_fd);
 }
@@ -353,8 +350,7 @@
   }
   cvd::subprocess({FLAGS_kernel_log_monitor_binary,
                    "-log_server_fd=" + std::to_string(server_fd),
-                   "-subscriber_fd=" + std::to_string(subscriber_fd),
-                   GetConfigFileArg()});
+                   "-subscriber_fd=" + std::to_string(subscriber_fd)});
   close(server_fd);
   if (subscriber_fd >= 0) {
     close(subscriber_fd);
@@ -383,8 +379,7 @@
   auto qemu_socket_arg = "-qemu_socket_fd=" + std::to_string(qemu_channel);
   auto client_socket_arg =
       "-client_socket_fd=" + std::to_string(client_channel);
-  cvd::subprocess({FLAGS_ivserver_binary, qemu_socket_arg, client_socket_arg,
-                   GetConfigFileArg()});
+  cvd::subprocess({FLAGS_ivserver_binary, qemu_socket_arg, client_socket_arg});
   close(qemu_channel);
   close(client_channel);
 }
@@ -400,8 +395,7 @@
   if (AdbTunnelEnabled()) {
     cvd::subprocess({FLAGS_socket_forward_proxy_binary,
                      GetGuestPortArg(),
-                     GetHostPortArg(),
-                     GetConfigFileArg()});
+                     GetHostPortArg()});
   }
 }
 
@@ -409,8 +403,7 @@
   if (FLAGS_start_vnc_server) {
     // Launch the vnc server, don't wait for it to complete
     auto port_options = "-port=" + std::to_string(FLAGS_vnc_server_port);
-    cvd::subprocess(
-        {FLAGS_vnc_server_binary, port_options, GetConfigFileArg()});
+    cvd::subprocess({FLAGS_vnc_server_binary, port_options});
   }
 }
 
@@ -492,8 +485,8 @@
   auto config = vsoc::CuttlefishConfig::Get();
   if (!config) {
     LOG(ERROR) << "Failed to instantiate config object. Most likely because "
-                  "config file was specified and doesn't exits: '"
-               << FLAGS_config_file << "'";
+                  "config file was specified and doesn't exist: '"
+               << getenv(vsoc::kCuttlefishConfigEnvVarName) << "'";
     return nullptr;
   }
   // Set this first so that calls to PerInstancePath below are correct
@@ -707,8 +700,8 @@
   if (invalid_manager) {
     return false;
   }
-  // Set the flag value to empty (in case the caller passed a value for it).
-  FLAGS_config_file = "";
+  // Set the env variable to empty (in case the caller passed a value for it).
+  unsetenv(vsoc::kCuttlefishConfigEnvVarName);
 
   ValidateAdbModeFlag();
 
@@ -919,6 +912,7 @@
   if (!config->SaveToFile(config_file)) {
     return LauncherExitCodes::kCuttlefishConfigurationSaveError;
   }
+  setenv(vsoc::kCuttlefishConfigEnvVarName, config_file.c_str(), true);
   if (symlink(config_file.c_str(), config_link.c_str()) != 0) {
     LOG(ERROR) << "Failed to create symlink to config file at " << config_link
                << ": " << strerror(errno);
diff --git a/host/libs/config/Android.bp b/host/libs/config/Android.bp
index 38d4ac0..b1ece2b 100644
--- a/host/libs/config/Android.bp
+++ b/host/libs/config/Android.bp
@@ -29,7 +29,6 @@
     ],
     static_libs: [
         "libxml2",
-        "libgflags",
         "libjsoncpp",
     ],
     defaults: ["cuttlefish_host_only"],
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index 0ebe82d..7adaa70 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -26,17 +26,12 @@
 #include <sstream>
 #include <string>
 
-#include <gflags/gflags.h>
 #include <glog/logging.h>
 #include <json/json.h>
 
 #include "common/libs/utils/environment.h"
 #include "common/libs/utils/files.h"
 
-DEFINE_string(config_file, vsoc::GetGlobalConfigFileLink(),
-              "A file from where to load the config values. This flag is "
-              "ignored by the launcher");
-
 namespace {
 
 int InstanceFromEnvironment() {
@@ -517,13 +512,15 @@
 }
 
 // Creates the (initially empty) config object and populates it with values from
-// the config file if the --config_file command line argument is present.
+// the config file if the CUTTLEFISH_CONFIG_FILE env variable is present.
 // Returns nullptr if there was an error loading from file
 /*static*/ CuttlefishConfig* CuttlefishConfig::BuildConfigImpl() {
   auto ret = new CuttlefishConfig();
-  if (ret && !FLAGS_config_file.empty()) {
-    auto loaded = ret->LoadFromFile(FLAGS_config_file.c_str());
+  char* config_file_cstr = getenv(kCuttlefishConfigEnvVarName);
+  if (ret && config_file_cstr && strlen(config_file_cstr) > 0) {
+    auto loaded = ret->LoadFromFile(config_file_cstr);
     if (!loaded) {
+      delete ret;
       return nullptr;
     }
   }
diff --git a/host/libs/config/cuttlefish_config.h b/host/libs/config/cuttlefish_config.h
index 6b24372..561e7c3 100644
--- a/host/libs/config/cuttlefish_config.h
+++ b/host/libs/config/cuttlefish_config.h
@@ -26,6 +26,7 @@
 namespace vsoc {
 
 constexpr char kDefaultUuidPrefix[] = "699acfc4-c8c4-11e7-882b-5065f31dc1";
+constexpr char kCuttlefishConfigEnvVarName[] = "CUTTLEFISH_CONFIG_FILE";
 
 // Holds the configuration of the cuttlefish instances.
 class CuttlefishConfig {