github.com/LanceLRQ/deer-common@v0.0.9-0.20210319081233-e8222ac018a8/provider/gcc.go (about)

     1  /* GCC Compiler Provider
     2   * (C) 2019 LanceLRQ
     3   *
     4   * This code is licenced under the GPLv3.
     5   */
     6  package provider
     7  
     8  import "fmt"
     9  
    10  type GnucCompileProvider struct {
    11  	CodeCompileProvider
    12  }
    13  
    14  type GnucppCompileProvider struct {
    15  	CodeCompileProvider
    16  }
    17  
    18  func NewGnucCompileProvider() *GnucCompileProvider {
    19  	return &GnucCompileProvider{
    20  		CodeCompileProvider{
    21  			isReady:  false,
    22  			realTime: false,
    23  			Name:     "gcc",
    24  		},
    25  	}
    26  }
    27  
    28  func (prov *GnucCompileProvider) Init(code string, workDir string) error {
    29  	prov.codeContent = code
    30  	prov.workDir = workDir
    31  
    32  	err := prov.checkWorkDir()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	err = prov.initFiles(".c", "")
    38  	return err
    39  }
    40  
    41  func (prov *GnucCompileProvider) Compile() (result bool, errmsg string) {
    42  	result, errmsg = prov.shell(fmt.Sprintf(CompileCommands.GNUC, prov.codeFilePath, prov.programFilePath))
    43  	if result {
    44  		prov.isReady = true
    45  	}
    46  	return
    47  }
    48  
    49  // 手动编译
    50  func (prov *GnucCompileProvider) ManualCompile(source string, target string, libraryDir []string) (bool, string) {
    51  	cmd := fmt.Sprintf(CompileCommands.GNUC, source, target)
    52  	if libraryDir != nil {
    53  		for _, v := range libraryDir {
    54  			cmd += fmt.Sprintf(" -I %s", v)
    55  		}
    56  	}
    57  	result, err := prov.shell(cmd)
    58  	return result, err
    59  }
    60  
    61  func (prov *GnucCompileProvider) GetRunArgs() (args []string) {
    62  	args = []string{prov.programFilePath}
    63  	return
    64  }
    65  
    66  func (prov *GnucCompileProvider) IsCompileError(remsg string) bool {
    67  	return false
    68  }
    69  
    70  func NewGnucppCompileProvider() *GnucppCompileProvider {
    71  	return &GnucppCompileProvider{
    72  		CodeCompileProvider{
    73  			isReady:  false,
    74  			realTime: false,
    75  			Name:     "g++",
    76  		},
    77  	}
    78  }
    79  
    80  func (prov *GnucppCompileProvider) Init(code string, workDir string) error {
    81  	prov.codeContent = code
    82  	prov.workDir = workDir
    83  
    84  	err := prov.checkWorkDir()
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	err = prov.initFiles(".cpp", "")
    90  	return err
    91  }
    92  
    93  func (prov *GnucppCompileProvider) Compile() (result bool, errmsg string) {
    94  	result, errmsg = prov.shell(fmt.Sprintf(CompileCommands.GNUCPP, prov.codeFilePath, prov.programFilePath))
    95  	if result {
    96  		prov.isReady = true
    97  	}
    98  	return
    99  }
   100  
   101  func (prov *GnucppCompileProvider) GetRunArgs() (args []string) {
   102  	args = []string{prov.programFilePath}
   103  	return
   104  }
   105  
   106  func (prov *GnucppCompileProvider) IsCompileError(remsg string) bool {
   107  	return false
   108  }
   109  
   110  // 手动编译
   111  func (prov *GnucppCompileProvider) ManualCompile(source string, target string, libraryDir []string) (bool, string) {
   112  	cmd := fmt.Sprintf(CompileCommands.GNUCPP, source, target)
   113  	if libraryDir != nil {
   114  		for _, v := range libraryDir {
   115  			cmd += fmt.Sprintf(" -I %s", v)
   116  		}
   117  	}
   118  	result, err := prov.shell(cmd)
   119  	return result, err
   120  }