From patchwork Wed Oct 31 00:38:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 195668 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id AB7F32C0084 for ; Wed, 31 Oct 2012 11:39:14 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1352248755; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=hbgM+W9 1hTXAuTgCtkFgHSfdXcY=; b=apm1uPIUFV5EnJyGU71V4+2xbAE9DVn66DWZXkB zVyyGCbS4+4cT1UoN2T2nJSB+oVtU+fwMxEl8BQsI7olp2gn4OiAJMLrqpj8M2CW JNabF4rUUx6OAlLLQcBS1G/VVNqjEgVcZvczQysLf3o9rAPZT8HbDS36Cm5S1WOY ktfQ= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=pZNxk+qLOPBLO9+ThxOqEnXj5hNZqsj8JLL1A4wuEF7M8D960vDRTbDnkBlHLU TFxHvyS7YdzCWHKGbwe28TR6Mk8NfXBnVYejhMjAVlwiBYVO6Xe89qfYQAm6eKl5 CjZraVPgV+dhIodcd9/mwwn9yVnZRmySqLAUKroTbanJE=; Received: (qmail 28631 invoked by alias); 31 Oct 2012 00:39:09 -0000 Received: (qmail 28622 invoked by uid 22791); 31 Oct 2012 00:39:08 -0000 X-SWARE-Spam-Status: No, hits=-5.1 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FILL_THIS_FORM_SHORT, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from mail-da0-f47.google.com (HELO mail-da0-f47.google.com) (209.85.210.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 31 Oct 2012 00:39:00 +0000 Received: by mail-da0-f47.google.com with SMTP id s35so348999dak.20 for ; Tue, 30 Oct 2012 17:38:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type:x-gm-message-state; bh=atesRKRB5nbOIA9zeQ8ymzLAQNgUjRs2UKo5owDasSU=; b=J1xokVxBwDzljufBCoLiZFsW7G6KXMqT9LEeszO75l0itX7AuG4rNKPtLtbsA5+qeI oD6jx3V8HSJJ2g/jZwRUDBm9NCT21F9i6uGapgbC6gEMXNkhP4sbnHni7qkcFydpEk2n g+B4Ft3RV1pDaTmXgZb58KBMTvpDHVD2L2QHTkW+HHwHHX0YTPvAUvQ1T9VzGYoyG9tD XJDD1sZ91KPizASDn60V48t/kq+RhCEpfzY9qIRlCOZDO7GIE43y8NLDh2qctqr7y/lS PFfh5W8iHq/9dvuCOM0tQnyXx4w3ALIwcn8BYOWwQamN/erJTG3Xdo0I0nW3urYSApNk g6DA== Received: by 10.68.221.225 with SMTP id qh1mr107916026pbc.50.1351643939715; Tue, 30 Oct 2012 17:38:59 -0700 (PDT) Received: from coign.google.com ([172.29.164.87]) by mx.google.com with ESMTPS id bc8sm1202364pab.5.2012.10.30.17.38.58 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 30 Oct 2012 17:38:59 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: Go patch committed: Add -fgo-relative-import-path option Date: Tue, 30 Oct 2012 17:38:56 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQn55CcmT6+681PweFhBi6nHo7+Tiq2e8+0YfX6Dye7TRHuoNYTrroxqtv0p6lCu919rs4jQ9g3p19GIeEz1LLO+2ZZUS2R/11wQS1qureZl1DWXLA3/luTQc9EFsSJdS/jhDi1vZkaAW4X1gXcY9jrjlMDkXMmQI3mbglWRD8+c/CvJoS/rprgK9TfVnmhWtYOTpuPN X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org The go tool, used by many people to build Go programs, handles relative import paths by passing a special option to the compiler. The option is a prefix to use for any relative import. The go tool does this so that programs can be built in a temporary directory and can still find packages imported on a relative path. The gc compiler calls this option -D. This patch adds the same option to gccgo, using the name -fgo-relative-import-path. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline and 4.7 branch. Ian 2012-10-30 Ian Lance Taylor * lang.opt (-fgo-relative-import-path): New option. * go-lang.c (go_relative_import_path): New static variable. (go_langhook_init): Pass go_relative_import_path to go_create_gogo. (go_langhook_handle_option): Handle -fgo-relative-import-path. * go-c.h (go_create_gogo): Update declaration. * gccgo.texi (Invoking gccgo): Document -fgo-relative-import-path. Index: gcc/go/lang.opt =================================================================== --- gcc/go/lang.opt (revision 192992) +++ gcc/go/lang.opt (working copy) @@ -61,6 +61,10 @@ fgo-prefix= Go Joined RejectNegative -fgo-prefix= Set package-specific prefix for exported Go names +fgo-relative-import-path= +Go Joined RejectNegative +-fgo-relative-import-path= Treat a relative import as relative to path + frequire-return-statement Go Var(go_require_return_statement) Init(1) Warning Functions which return values must end with return statements Index: gcc/go/gccgo.texi =================================================================== --- gcc/go/gccgo.texi (revision 192992) +++ gcc/go/gccgo.texi (working copy) @@ -184,6 +184,12 @@ Using either @option{-fgo-pkgpath} or @o the special treatment of the @code{main} package and permits that package to be imported like any other. +@item -fgo-relative-import-path=@var{dir} +@cindex @option{-fgo-relative-import-path} +A relative import is an import that starts with @file{./} or +@file{../}. If this option is used, @command{gccgo} will use +@var{dir} as a prefix for the relative import when searching for it. + @item -frequire-return-statement @itemx -fno-require-return-statement @cindex @option{-frequire-return-statement} Index: gcc/go/gofrontend/gogo.cc =================================================================== --- gcc/go/gofrontend/gogo.cc (revision 192992) +++ gcc/go/gofrontend/gogo.cc (working copy) @@ -44,6 +44,7 @@ Gogo::Gogo(Backend* backend, Linemap* li pkgpath_set_(false), pkgpath_from_option_(false), prefix_from_option_(false), + relative_import_path_(), verify_types_(), interface_types_(), specific_type_functions_(), @@ -477,7 +478,8 @@ Gogo::import_package(const std::string& return; } - Import::Stream* stream = Import::open_package(filename, location); + Import::Stream* stream = Import::open_package(filename, location, + this->relative_import_path_); if (stream == NULL) { error_at(location, "import file %qs not found", filename.c_str()); Index: gcc/go/gofrontend/gogo.h =================================================================== --- gcc/go/gofrontend/gogo.h (revision 192992) +++ gcc/go/gofrontend/gogo.h (working copy) @@ -206,6 +206,17 @@ class Gogo pkgpath_from_option() const { return this->pkgpath_from_option_; } + // Return the relative import path as set from the command line. + // Returns an empty string if it was not set. + const std::string& + relative_import_path() const + { return this->relative_import_path_; } + + // Set the relative import path from a command line option. + void + set_relative_import_path(const std::string& s) + {this->relative_import_path_ = s; } + // Return the priority to use for the package we are compiling. // This is two more than the largest priority of any package we // import. @@ -732,6 +743,9 @@ class Gogo bool pkgpath_from_option_; // Whether an explicit prefix was set by -fgo-prefix. bool prefix_from_option_; + // The relative import path, from the -fgo-relative-import-path + // option. + std::string relative_import_path_; // A list of types to verify. std::vector verify_types_; // A list of interface types defined while parsing. Index: gcc/go/gofrontend/go.cc =================================================================== --- gcc/go/gofrontend/go.cc (revision 192992) +++ gcc/go/gofrontend/go.cc (working copy) @@ -21,7 +21,7 @@ static Gogo* gogo; GO_EXTERN_C void go_create_gogo(int int_type_size, int pointer_size, const char *pkgpath, - const char *prefix) + const char *prefix, const char *relative_import_path) { go_assert(::gogo == NULL); Linemap* linemap = go_get_linemap(); @@ -32,6 +32,9 @@ go_create_gogo(int int_type_size, int po else if (prefix != NULL) ::gogo->set_prefix(prefix); + if (relative_import_path != NULL) + ::gogo->set_relative_import_path(relative_import_path); + // FIXME: This should be in the gcc dependent code. ::gogo->define_builtin_function_trees(); } Index: gcc/go/gofrontend/import.h =================================================================== --- gcc/go/gofrontend/import.h (revision 192992) +++ gcc/go/gofrontend/import.h (working copy) @@ -124,8 +124,10 @@ class Import // Find import data. This searches the file system for FILENAME and // returns a pointer to a Stream object to read the data that it // exports. LOCATION is the location of the import statement. + // RELATIVE_IMPORT_PATH is used as a prefix for a relative import. static Stream* - open_package(const std::string& filename, Location location); + open_package(const std::string& filename, Location location, + const std::string& relative_import_path); // Constructor. Import(Stream*, Location); Index: gcc/go/gofrontend/import.cc =================================================================== --- gcc/go/gofrontend/import.cc (revision 192992) +++ gcc/go/gofrontend/import.cc (working copy) @@ -41,6 +41,9 @@ go_add_search_path(const char* path) // When FILENAME is not an absolute path and does not start with ./ or // ../, we use the search path provided by -I and -L options. +// When FILENAME does start with ./ or ../, we use +// RELATIVE_IMPORT_PATH as a prefix. + // When FILENAME does not exist, we try modifying FILENAME to find the // file. We use the first of these which exists: // * We append ".gox". @@ -55,19 +58,35 @@ go_add_search_path(const char* path) // later in the search path. Import::Stream* -Import::open_package(const std::string& filename, Location location) +Import::open_package(const std::string& filename, Location location, + const std::string& relative_import_path) { bool is_local; if (IS_ABSOLUTE_PATH(filename)) is_local = true; - else if (filename[0] == '.' && IS_DIR_SEPARATOR(filename[1])) + else if (filename[0] == '.' + && (filename[1] == '\0' || IS_DIR_SEPARATOR(filename[1]))) is_local = true; else if (filename[0] == '.' && filename[1] == '.' - && IS_DIR_SEPARATOR(filename[2])) + && (filename[2] == '\0' || IS_DIR_SEPARATOR(filename[2]))) is_local = true; else is_local = false; + + std::string fn = filename; + if (is_local && !IS_ABSOLUTE_PATH(filename) && !relative_import_path.empty()) + { + if (fn == ".") + { + // A special case. + fn = relative_import_path; + } + else + fn = relative_import_path + '/' + fn; + is_local = false; + } + if (!is_local) { for (std::vector::const_iterator p = search_path.begin(); @@ -77,14 +96,14 @@ Import::open_package(const std::string& std::string indir = *p; if (!indir.empty() && indir[indir.size() - 1] != '/') indir += '/'; - indir += filename; + indir += fn; Stream* s = Import::try_package_in_directory(indir, location); if (s != NULL) return s; } } - Stream* s = Import::try_package_in_directory(filename, location); + Stream* s = Import::try_package_in_directory(fn, location); if (s != NULL) return s; Index: gcc/go/go-lang.c =================================================================== --- gcc/go/go-lang.c (revision 192992) +++ gcc/go/go-lang.c (working copy) @@ -85,6 +85,7 @@ struct GTY(()) language_function static const char *go_pkgpath = NULL; static const char *go_prefix = NULL; +static const char *go_relative_import_path = NULL; /* Language hooks. */ @@ -101,7 +102,8 @@ go_langhook_init (void) to, e.g., unsigned_char_type_node) but before calling build_common_builtin_nodes (because it calls, indirectly, go_type_for_size). */ - go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix); + go_create_gogo (INT_TYPE_SIZE, POINTER_SIZE, go_pkgpath, go_prefix, + go_relative_import_path); build_common_builtin_nodes (); @@ -240,6 +242,10 @@ go_langhook_handle_option ( go_prefix = arg; break; + case OPT_fgo_relative_import_path_: + go_relative_import_path = arg; + break; + default: /* Just return 1 to indicate that the option is valid. */ break; Index: gcc/go/go-c.h =================================================================== --- gcc/go/go-c.h (revision 192992) +++ gcc/go/go-c.h (working copy) @@ -33,7 +33,8 @@ extern int go_enable_optimize (const cha extern void go_add_search_path (const char*); extern void go_create_gogo (int int_type_size, int pointer_size, - const char* pkgpath, const char *prefix); + const char* pkgpath, const char *prefix, + const char *relative_import_path); extern void go_parse_input_files (const char**, unsigned int, bool only_check_syntax,