github.com/hwaf/hwaf@v0.0.0-20140814122253-5465f73b20f1/test_hwaf_projects_test.go (about) 1 package main_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 "text/template" 11 ) 12 13 type pkgdef_t struct { 14 Name string 15 Deps []string 16 } 17 18 func (p pkgdef_t) BaseName() string { 19 return filepath.Base(p.Name) 20 } 21 22 func (p pkgdef_t) TestName() string { 23 return "test" + p.BaseName() 24 } 25 26 func (p pkgdef_t) LibName() string { 27 return "Lib" + p.BaseName() 28 } 29 30 func (p pkgdef_t) PkgDeps() string { 31 if len(p.Deps) <= 0 { 32 return "" 33 } 34 s := []string{ 35 "deps: {", 36 " public: [", 37 } 38 for _, dep := range p.Deps { 39 s = append( 40 s, 41 " "+dep+",", 42 ) 43 } 44 s = append( 45 s, 46 " ],", 47 "},", 48 ) 49 // reindent 50 for i := range s { 51 s[i] = " " + s[i] 52 } 53 return strings.Join(s, "\n") 54 } 55 56 func (p pkgdef_t) LibUse() string { 57 if len(p.Deps) <= 0 { 58 return "" 59 } 60 s := []string{ 61 "use: [", 62 } 63 for _, dep := range p.Deps { 64 s = append( 65 s, 66 fmt.Sprintf(` "Lib%s",`, dep), 67 ) 68 } 69 s = append( 70 s, 71 "],", 72 ) 73 // reindent 74 for i := range s { 75 s[i] = " " + s[i] 76 } 77 return strings.Join(s, "\n") 78 } 79 80 func (p pkgdef_t) HdrIncludes() string { 81 if len(p.Deps) <= 0 { 82 return "" 83 } 84 s := make([]string, 0, len(p.Deps)) 85 for _, dep := range p.Deps { 86 s = append( 87 s, 88 fmt.Sprintf(`#include "%s/Lib%s.hxx"`, dep, dep), 89 ) 90 } 91 return strings.Join(s, "\n") 92 } 93 94 func (p pkgdef_t) HdrMembers() string { 95 if len(p.Deps) <= 0 { 96 return "" 97 } 98 s := make([]string, 0, len(p.Deps)) 99 for _, dep := range p.Deps { 100 s = append( 101 s, 102 fmt.Sprintf(` CLib%s m_%s;`, dep, dep), 103 ) 104 } 105 return strings.Join(s, "\n") 106 } 107 108 func (p pkgdef_t) LibMembers() string { 109 if len(p.Deps) <= 0 { 110 return "" 111 } 112 s := make([]string, 0, len(p.Deps)) 113 for _, dep := range p.Deps { 114 s = append( 115 s, 116 fmt.Sprintf(` m_%s.f();`, dep), 117 ) 118 } 119 return strings.Join(s, "\n") 120 } 121 122 func (p pkgdef_t) TestUse() string { 123 if len(p.Deps) <= 0 { 124 return "use: [" + p.LibName() + "]," 125 } 126 s := []string{ 127 "use: [", 128 } 129 for _, dep := range p.Deps { 130 s = append( 131 s, 132 fmt.Sprintf(` "Lib%s",`, dep), 133 ) 134 } 135 s = append( 136 s, 137 fmt.Sprintf(` "%s",`, p.LibName()), 138 "],", 139 ) 140 // reindent 141 for i := range s { 142 s[i] = " " + s[i] 143 } 144 return strings.Join(s, "\n") 145 } 146 147 func TestMultiProject(t *testing.T) { 148 workdir, err := ioutil.TempDir("", "hwaf-test-") 149 if err != nil { 150 t.Fatalf(err.Error()) 151 } 152 defer os.RemoveAll(workdir) 153 //fmt.Printf(">>> test: %s\n", workdir) 154 155 err = os.Chdir(workdir) 156 if err != nil { 157 t.Fatalf(err.Error()) 158 } 159 160 hwaf, err := newlogger("hwaf.log") 161 if err != nil { 162 t.Fatalf(err.Error()) 163 } 164 defer hwaf.Close() 165 166 const hscript_tmpl = ` 167 ## -*- yaml -*- 168 169 package: { 170 name: "{{.Name}}", 171 authors: ["me"], 172 {{.PkgDeps}} 173 } 174 175 configure: { 176 tools: ["compiler_c", "compiler_cxx", "find_python"], 177 env: { 178 PYTHONPATH: "${INSTALL_AREA}/python:${PYTHONPATH}", 179 }, 180 } 181 182 build: { 183 {{.TestName}}: { 184 features: "cxx cxxprogram hwaf_install_headers hwaf_utest", 185 includes: "includes", 186 export_includes: "includes", 187 cwd: "includes", 188 source: "src/{{.TestName}}.cxx", 189 {{.TestUse}} 190 }, 191 192 {{.LibName}}: { 193 features: "cxx cxxshlib hwaf_install_headers hwaf_export_lib", 194 includes: "includes", 195 export_includes: "includes", 196 cwd: "includes", 197 source: "src/{{.LibName}}.cxx", 198 {{.LibUse}} 199 }, 200 } 201 ` 202 203 const src_lib_tmpl = ` 204 #include <iostream> 205 #include "{{.BaseName}}/{{.LibName}}.hxx" 206 207 C{{.LibName}}::C{{.LibName}}() 208 { 209 std::cout << "c-tor C{{.LibName}}" << std::endl; 210 } 211 212 C{{.LibName}}::~C{{.LibName}}() 213 { 214 std::cout << "d-tor C{{.LibName}}" << std::endl; 215 } 216 217 void 218 C{{.LibName}}::f() 219 { 220 std::cout << "C{{.LibName}}.f" << std::endl; 221 {{.LibMembers}} 222 } 223 ` 224 225 const hdr_lib_tmpl = ` 226 #ifndef __{{.LibName}}_hxx__ 227 #define __{{.LibName}}_hxx__ 1 228 229 // -------------------------------------- 230 {{.HdrIncludes}} 231 232 #ifdef _MSC_VER 233 #define DllExport __declspec( dllexport ) 234 #else 235 #define DllExport 236 #endif 237 238 class DllExport C{{.LibName}} 239 { 240 public: 241 C{{.LibName}}(); 242 ~C{{.LibName}}(); 243 void f(); 244 private: 245 {{.HdrMembers}} 246 }; 247 248 #endif // !__{{.LibName}}_hxx__ 249 ` 250 const src_tst_tmpl = ` 251 #include <iostream> 252 #include "{{.BaseName}}/{{.LibName}}.hxx" 253 254 int main(int argc, char **argv) 255 { 256 std::cout << "Testing binary for package {{.Name}}\n" 257 << "argc: " << argc << "\n" 258 << "argv: " << argv << "\n"; 259 260 C{{.LibName}} o; 261 o.f(); 262 return 0; 263 } 264 ` 265 266 gen_tmpl := func(fname string, text string, data interface{}) error { 267 f, err := os.Create(fname) 268 if err != nil { 269 return err 270 } 271 defer f.Close() 272 t := template.New("tmpl") 273 template.Must(t.Parse(text)) 274 err = t.Execute(f, data) 275 if err != nil { 276 return err 277 } 278 return f.Sync() 279 } 280 281 gen_proj := func(projname string, projdeps []string, pkgdefs []pkgdef_t) error { 282 var err error 283 projdir := filepath.Join(workdir, projname) 284 err = os.MkdirAll(projdir, 0777) 285 if err != nil { 286 return err 287 } 288 289 pkgdir := filepath.Join(projdir, "src") 290 err = os.MkdirAll(pkgdir, 0777) 291 if err != nil { 292 return err 293 } 294 295 gen_pkg := func(pkg pkgdef_t) error { 296 var err error 297 // create pkg structure 298 for _, dir := range []string{ 299 filepath.Join(pkg.Name, "includes", pkg.Name), 300 filepath.Join(pkg.Name, "src"), 301 } { 302 err = os.MkdirAll(dir, 0777) 303 if err != nil { 304 return err 305 } 306 } 307 308 // create hscript 309 fname := filepath.Join(pkg.Name, "hscript.yml") 310 err = gen_tmpl(fname, hscript_tmpl, pkg) 311 if err != nil { 312 return err 313 } 314 315 // header 316 fname = filepath.Join(pkg.Name, "includes", pkg.Name, fmt.Sprintf("%s.hxx", pkg.LibName())) 317 err = gen_tmpl(fname, hdr_lib_tmpl, pkg) 318 if err != nil { 319 return err 320 } 321 322 // lib 323 fname = filepath.Join(pkg.Name, "src", fmt.Sprintf("%s.cxx", pkg.LibName())) 324 err = gen_tmpl(fname, src_lib_tmpl, pkg) 325 if err != nil { 326 return err 327 } 328 329 // test 330 fname = filepath.Join(pkg.Name, "src", fmt.Sprintf("%s.cxx", pkg.TestName())) 331 err = gen_tmpl(fname, src_tst_tmpl, pkg) 332 if err != nil { 333 return err 334 } 335 336 return err 337 } 338 339 //fmt.Printf("pkgdir=%q\n", pkgdir) 340 err = os.Chdir(pkgdir) 341 if err != nil { 342 return err 343 } 344 345 for _, pkg := range pkgdefs { 346 err = gen_pkg(pkg) 347 if err != nil { 348 return err 349 } 350 } 351 352 //fmt.Printf("projdir=%q\n", projdir) 353 err = os.Chdir(projdir) 354 if err != nil { 355 return err 356 } 357 358 gen_projdeps := func(projdeps []string) string { 359 if len(projdeps) <= 0 { 360 return "" 361 } 362 projpath := make([]string, 0, len(projdeps)) 363 for _, dep := range projdeps { 364 projpath = append( 365 projpath, 366 filepath.Join(workdir, dep, "install-area"), 367 ) 368 } 369 return strings.Join(projpath, ":") 370 } 371 // build project 372 for _, cmd := range [][]string{ 373 {"hwaf", "init", "-v=1", "."}, 374 {"hwaf", "setup", "-v=1", "-p=" + gen_projdeps(projdeps)}, 375 {"hwaf", "configure"}, 376 {"hwaf"}, 377 {"hwaf", "check"}, 378 } { 379 err := hwaf.Run(cmd[0], cmd[1:]...) 380 if err != nil { 381 hwaf.Display() 382 t.Fatalf("cmd %v failed: %v", cmd, err) 383 } 384 } 385 return err 386 } 387 388 for _, table := range []struct { 389 projname string 390 projdeps []string 391 pkgdefs []pkgdef_t 392 }{ 393 { 394 "project_001", 395 []string{}, 396 []pkgdef_t{ 397 { 398 "project_001_pkg_001", 399 []string{}, 400 }, 401 { 402 "project_001_pkg_002", 403 []string{}, 404 }, 405 { 406 "project_001_pkg_003", 407 []string{}, 408 }, 409 }, 410 }, 411 412 { 413 "project_002", 414 []string{"project_001"}, 415 []pkgdef_t{ 416 { 417 "project_002_pkg_001", 418 []string{"project_001_pkg_001"}, 419 }, 420 { 421 "project_002_pkg_002", 422 []string{"project_001_pkg_001", "project_001_pkg_002"}, 423 }, 424 { 425 "project_002_pkg_003", 426 []string{"project_001_pkg_001", "project_001_pkg_003"}, 427 }, 428 }, 429 }, 430 431 { 432 "project_003", 433 []string{"project_001"}, 434 []pkgdef_t{ 435 { 436 "project_003_pkg_001", 437 []string{"project_001_pkg_001"}, 438 }, 439 { 440 "project_003_pkg_002", 441 []string{"project_001_pkg_001", "project_001_pkg_002"}, 442 }, 443 { 444 "project_003_pkg_003", 445 []string{"project_001_pkg_001", "project_001_pkg_003"}, 446 }, 447 }, 448 }, 449 450 { 451 "project_004", 452 []string{"project_002", "project_003"}, 453 []pkgdef_t{ 454 { 455 "project_004_pkg_001", 456 []string{ 457 "project_001_pkg_001", 458 "project_002_pkg_001", 459 }, 460 }, 461 { 462 "project_004_pkg_002", 463 []string{ 464 "project_001_pkg_001", 465 "project_001_pkg_002", 466 "project_002_pkg_001", 467 "project_003_pkg_002", 468 }, 469 }, 470 { 471 "project_004_pkg_003", 472 []string{ 473 "project_001_pkg_001", 474 "project_001_pkg_003", 475 "project_002_pkg_001", 476 "project_002_pkg_003", 477 "project_003_pkg_001", 478 "project_003_pkg_003", 479 }, 480 }, 481 }, 482 }, 483 } { 484 err = gen_proj(table.projname, table.projdeps, table.pkgdefs) 485 if err != nil { 486 t.Fatalf("project [%v]: %v\n", table.projname, err) 487 } 488 } 489 490 //hwaf.Display() 491 } 492 493 // EOF