Skip to content

Commit

Permalink
refs simelo#17 Merge branch 'master' of https://github.com/simelo/cgogen
Browse files Browse the repository at this point in the history
 into stdevHan_t17_support_callback
  • Loading branch information
Maykel Arias Torres committed Feb 3, 2020
2 parents 12cff6b + 291e1e4 commit 22b0844
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Add parameter `prefix` to define prefix the functions and errors generations

### Fixed

### Changed

### Removed
59 changes: 47 additions & 12 deletions src/cmd/cgogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Config struct {
FullTranspile bool //Full conversion to c code
FullTranspileDir string
FullTranspileOut string
MainPackagePath string
PrefixLib string
}

func (c *Config) register() {
Expand All @@ -50,6 +52,8 @@ func (c *Config) register() {
flag.BoolVar(&c.IgnoreDependants, "id", false, "Ignore dependants")
flag.StringVar(&c.FullTranspileDir, "transdir", "", "Directory to get source code for full transpile")
flag.StringVar(&c.FullTranspileOut, "transout", "", "Directory to put c files of full transpile")
flag.StringVar(&c.MainPackagePath, "main", "", "Define main package path the functions")
flag.StringVar(&c.PrefixLib, "prefix", "SKY", "Define prefix the function and type error export")
}

var (
Expand All @@ -62,14 +66,23 @@ var (
//Map of types that will replaced by custom types
var customTypesMap = make(map[string]string)

// Configure prefix
var (
functionPrefix = ""
includePrefix = ""
)

//Types that will use functions of type inplace to convert
var inplaceConvertTypesPackages = map[string]string{
"PubKeySlice": "cipher",
"Address": "cipher",
"BalanceResult": "cli",
}

var mainPackagePath = string("github.com/SkycoinProject/skycoin/src/")
var (
mainPackagePath = ""
packagePath = ""
)

var arrayTypes = map[string]string{
"PubKey": "cipher",
Expand All @@ -93,6 +106,14 @@ func main() {

cfg.register()
flag.Parse()
if cfg.MainPackagePath == "" {
fmt.Println("The main package path is required")
return
}
packagePath, mainPackagePath = getPathPackage(cfg.MainPackagePath)
functionPrefix = strings.ToUpper(string(cfg.PrefixLib))
includePrefix = strings.ToLower(cfg.PrefixLib)
log.Println("Load prefix " + functionPrefix)

if cfg.Verbose {
applog = log.Printf
Expand Down Expand Up @@ -148,8 +169,7 @@ func doGoFile() {
#include <string.h>
#include <stdlib.h>
#include "skytypes.h"
#include "skyfee.h"`)
#include "` + includePrefix + `types.h"`)
}

typeDefs := make([]*ast.GenDecl, 0)
Expand Down Expand Up @@ -305,10 +325,10 @@ func findImportPath(importName string) (string, bool) {
return "", false
}

func isSkycoinName(importName string) bool {
func isLibName(importName string) bool {
path, result := findImportPath(importName)
if result {
return strings.HasPrefix(path, "github.com/SkycoinProject")
return strings.HasPrefix(path, packagePath)
} else {
return false
}
Expand Down Expand Up @@ -403,7 +423,7 @@ func typeSpecStr(_typeExpr *ast.Expr, packageName string, isOutput bool) (string
} else if isInCustomTypesList(externPackage + "." + typeName) {
spec = getCustomTypeName(externPackage + "." + typeName)
isDealt = true
} else if !isSkycoinName(externPackage) {
} else if !isLibName(externPackage) {
return externPackage, false
}
}
Expand Down Expand Up @@ -618,7 +638,7 @@ func processFunc(fast *ast.File, fdecl *ast.FuncDecl, outFile *jen.File, dependa
}
}

cfuncName := "SKY_" + fast.Name.Name + "_" + funcName
cfuncName := functionPrefix + "_" + fast.Name.Name + "_" + funcName
stmt := outFile.Comment("export " + cfuncName) //nolint staticcheck
stmt = outFile.Func().Id(cfuncName)
stmt = stmt.Params(params...)
Expand Down Expand Up @@ -725,7 +745,7 @@ func getLookupHandleCode(name string, typeName string, isPointer bool) []jen.Cod
lookUpName := "lookup" + handleTypes[typeName] + "Handle"
listVar = listVar.Id(lookUpName).Call(jen.Op("*").Id(argName(name)))
checkError := jen.If(jen.Op("!").Id("ok"+name)).
Block(jen.Id(returnVarName).Op("=").Id("SKY_BAD_HANDLE"), jen.Return())
Block(jen.Id(returnVarName).Op("=").Id(functionPrefix+"_BAD_HANDLE"), jen.Return())
if !isPointer {
assign := jen.Id(name).Op(":=").Op("*").Id(varname)
return jenCodeToArray(listVar, checkError, assign)
Expand Down Expand Up @@ -846,7 +866,7 @@ func getCodeToConvertOutParameter(_typeExpr *ast.Expr, package_name string, name
argCode = jen.Op("&").Id(argName(name))
}
return jen.Op("*").Id(name).Op("=").Id("register" + handleTypes[package_name+packageSeparator+typeName] + "Handle").Call(argCode)
} else if isSkyArrayType(typeName) {
} else if isLibArrayType(typeName) {
var argCode jen.Code
if isPointer {
argCode = jen.Parens(jen.Op("*").Id(argName(name))).Op("[:]")
Expand Down Expand Up @@ -902,7 +922,7 @@ func getCodeToConvertOutParameter(_typeExpr *ast.Expr, package_name string, name
return nil
}

func isSkyArrayType(typeName string) bool {
func isLibArrayType(typeName string) bool {
if _, ok := arrayTypes[typeName]; ok {
return true
}
Expand Down Expand Up @@ -1049,7 +1069,7 @@ func processTypeExpression(fast *ast.File, type_expr ast.Expr,
typeCode, isBasic := GetCTypeFromGoType(identExpr.Name)
if !isBasic {
addDependency := false
if packageName != fast.Name.Name && !isSkycoinName(packageName) {
if packageName != fast.Name.Name && !isLibName(packageName) {
if cfg.DependOnlyExternal {
if isExternalName(packageName) {
addDependency = true
Expand Down Expand Up @@ -1226,7 +1246,7 @@ func fixExportComment(filePath string) {
err = f.Close()
check(err)

contents = strings.Replace(contents, "// export SKY_", "//export SKY_", -1)
contents = strings.Replace(contents, "// export "+functionPrefix+"_", "//export "+functionPrefix+"_", -1)
f, err = os.Create(filePath)
check(err)
_, err = f.WriteString(contents)
Expand Down Expand Up @@ -1325,3 +1345,18 @@ func getCallbackCode(varName string) *jen.Statement {

return varFunction
}

func getPathPackage(path string) (packagePath_ string, mainPackagePath_ string) {

index := strings.LastIndex(path, "/")
if index == -1 {
mainPackagePath_ = path
} else {
mainPackagePath_ = string(path[:index])
}
index = strings.LastIndex(mainPackagePath_, "/")
packagePath_ = string(mainPackagePath_[:index+1])
mainPackagePath_ = mainPackagePath_ + "/src/"

return
}

0 comments on commit 22b0844

Please sign in to comment.