Class: MxxRu::Cpp::LibOrDllTarget

Inherits:
Target show all
Defined in:
lib/mxx_ru/cpp/target.rb

Overview

Base class for targets that may be lib or dll, or MacOS bundle.

In that cases for a project should be defined special target class. The class constructor should define two blocks:

  • one of them is called if developer wants to build target as a static library. Block is run during as_lib method call;

  • second is called if developer wants to build target as a shared library. Block is run during as_dll method call;

Starting from version 1.4.10 this approach could also be used for bulding MacOS bundles.

For example:

class Prj < MxxRu::Cpp::LibOrDllTarget
  TAG = "threads_1/prj.rb"
  def initialize( a_alias = TAG )
    super( a_alias, TAG )

    target( "threads.1.3" )

    threading_mode( MxxRu::Cpp::THREADING_MULTI )

    init_dll_block(
      Proc.new {
        rtl_mode( MxxRu::Cpp::RTL_SHARED )
        implib_path( "lib" )
        define( "THREADS_1__DLL", OPT_UPSPREAD )
      })
    # This is avaliable from v.1.4.10
    init_macos_bundle_block(
      Proc.new {
        rtl_mode( MxxRu::Cpp::RTL_SHARED )
      }
    )

    cpp_source( "threads.cpp" )
    cpp_source( "micro_time.cpp" )

    if toolset.tag( "target_os" ) == "mswin"
      define( "__WIN32__" )
      cpp_source( "win32/os_thread.cpp" )
    elsif toolset.tag( "target_os" ) == "unix"
      cpp_source( "posix/os_thread.cpp" )
    end

    define( "THREADS_1__PRJ" )
  end
end

After definition of such class for the target, project may allow to use itself by the other projects using two methods.

Manual execution of as_lib, as_dll, as_macos_bundle methods

If project defines one class only, inherited from LibOrDllTarget, then all clients of that project should explicitly call as_lib or as_dll, or as_macos_bundle method during a reference to required_prj:

class MyPrj < MxxRu::Cpp::ExeTarget
  def initialize( a_alias = "my_prj.rb" )
    super( a_alias )
    required_prj( "threads_1/prj.rb" ).as_dll( self )
  end
end

Such approach is not good because if in composite project several projects at the same time uses threads_1/prj.rb, then in one of them it’s easy to forget to call as_dll. And it wouldn’t be incorrect, because as_dll may be called by other projects in composite project.

Definition of auxiliary target classes

To make usage of target that may be lib or dll easier, project may define two auxiliary classes, which would call methods as_lib, as_dll and as_macos_bundle inside their constructors:

class Prj < MxxRu::Cpp::LibOrDllTarget
  def initialize( a_alias = "threads_1/prj.rb" )
    super( a_alias, "threads_1" )
    ...
  end
end
class Lib < Prj
  def super( a_alias = "threads_1/lib.rb" )
    super( a_alias )
    as_lib
  end
end
class Dll < Prj
  def super( a_alias = "threads_1/dll.rb" )
    super( a_alias )
    as_dll
  end
end
class MacOSBundle < Prj
  def super( a_alias = "threads_1/macos_bundle.rb" )
    super( a_alias )
    as_macos_bundle
  end
end

Then for using the target it would be enough to do that:

class MyPrj < MxxRu::Cpp::ExeTarget
  def initialize( a_alias = "my_prj.rb" )
    super( a_alias )
    required_prj( "threads_1/dll.rb" )
  end
end

Direct Known Subclasses

DllTarget, LibTarget, MacOSBundleTarget

Defined Under Namespace

Classes: TypeAlreadyDefinedEx, TypeNotDefinedEx

Constant Summary collapse

Type_not_defined_ex =

For compatibility with previous versions.

TypeNotDefinedEx
Type_already_defined_ex =

For compatibility with previous versions.

TypeAlreadyDefinedEx
@@mxx_tags =

Map describing unique target identifiers and their types. Used to avoid conflicts, when the same target is inside composite project as both lib and dll.

Hash.new

Constants inherited from Target

Target::Global_obj_placement_info, Target::OPT_LOCAL, Target::OPT_UPSPREAD

Instance Attribute Summary

Attributes inherited from Target

#mxx_c_files, #mxx_cpp_files, #mxx_encoding, #mxx_implib_path, #mxx_mswin_rc_file, #mxx_mswin_res_file, #mxx_obj_files, #mxx_optimization, #mxx_screen_mode, #mxx_sources_root, #mxx_target_ext, #mxx_target_name, #mxx_target_prefix, #mxx_target_root, #vc8_actual_manifest

Attributes inherited from AbstractTarget

#mxx_full_targets_names, #mxx_generators, #mxx_required_prjs

Instance Method Summary collapse

Methods inherited from Target

#build, #c_source, #clean, #cpp_source, #create_full_result_target_file_name, #create_full_src_file_name, #create_full_target_file_name, define_spreadable_option_methods, #global_obj_placement, global_option_methods, #implib_path, initialize_spreadable_option_instance_variables, #mswin_rc_file, #mswin_res_file, #mxx_obj_placement, #mxx_runtime_mode, #obj_file, #obj_placement, #optimization, #reset, #screen_mode, #source_encoding, #sources_root, #target, #target_ext, #target_prefix, #target_root, #toolset, #vc8_source_manifest, #vc8_source_manifest=

Methods inherited from BinaryTarget

check_libraries_types, #lib, #lib_shared, #lib_static, #mxx_add_required_lib, #mxx_add_required_lib_path, #mxx_required_lib_paths, #mxx_required_libs

Methods inherited from AbstractTarget

#build, #clean, define_plural_form_method, #generator, #mxx_add_full_target_name, #prj_alias, #required_prj, #reset, run

Constructor Details

#initialize(a_prj_alias, a_prj_tag) ⇒ LibOrDllTarget

Constructor.

a_prj_alias

Project alias.

a_prj_tag

Unique value, which identifies target given. This value used to avoid conflicts in the next situations:

class Prj < MxxRu::Cpp::LibOrDllTarget ... end
class Lib < Prj ... end
class Dll < Prj ... end
class MacOSBundle < Prj ... end
class MyExe < MxxRu::Cpp::ExeTarget
  def initialize( a_alias = "my_exe.rb" )
    super( a_alias )
    required_prj( "threads_1/dll.rb" )
  end
end
class MyDll < MxxRu::Cpp::DllTarget
  def initialize( a_alias = "my_dll.rb" )
    super( a_alias )
    required_prj( "threads_1/lib.rb" )
  end
end

If both MyExe and MyDll are used in one composite project, then it’s a conflict of usage of threads_1/dll.rb and threads_1/lib.rb.



1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
# File 'lib/mxx_ru/cpp/target.rb', line 1391

def initialize( a_prj_alias, a_prj_tag )
  super( a_prj_alias )

  @mxx_prj_tag = a_prj_tag

  # Block, which is run if lib is a target.
  @mxx_init_lib_block = nil
  # Block, which is run if dll is a target.
  @mxx_init_dll_block = nil
  # Block, which is run if MacOS bundle is a target.
  @mxx_init_macos_bundle_block = nil
  # Target type should be defined or by as_lib method, or by as_dll method,
  # or by as_macos_bundle.
  @mxx_target_type = nil

end

Instance Method Details

#as_dllObject

Set dll as target type.



1419
1420
1421
1422
1423
1424
1425
1426
# File 'lib/mxx_ru/cpp/target.rb', line 1419

def as_dll
  if ensure_type_not_set_yet( DllTargetType::TYPE )
    @mxx_target_type = DllTargetType.new
    if nil != @mxx_init_dll_block
      @mxx_init_dll_block.call
    end
  end
end

#as_libObject

Set lib as target type.



1409
1410
1411
1412
1413
1414
1415
1416
# File 'lib/mxx_ru/cpp/target.rb', line 1409

def as_lib
  if ensure_type_not_set_yet( LibTargetType::TYPE )
    @mxx_target_type = LibTargetType.new
    if nil != @mxx_init_lib_block
      @mxx_init_lib_block.call
    end
  end
end

#as_macos_bundleObject

Set MacOS bundle as target type.



1429
1430
1431
1432
1433
1434
1435
1436
# File 'lib/mxx_ru/cpp/target.rb', line 1429

def as_macos_bundle
  if ensure_type_not_set_yet( MacOSBundleTargetType::TYPE )
    @mxx_target_type = MacOSBundleTargetType.new
    if nil != @mxx_init_macos_bundle_block
      @mxx_init_macos_bundle_block.call
    end
  end
end

#target_typeObject

Return current value of mxx_target_type.



1439
1440
1441
1442
1443
1444
1445
# File 'lib/mxx_ru/cpp/target.rb', line 1439

def target_type
  if nil == @mxx_target_type
    raise TypeNotDefinedEx.new( prj_alias )
  end

  return @mxx_target_type
end