Repository: pytorch/text

Issue:
Should hide the symbols from the third party
I am integrating KenLM in torchaudio and realized that KenLM uses double-conversion like torchtext does.

In torchaudio we are hiding the symbols of third party with `-fhidden` flag with compiling, but it turns out that torchtext does not do this. (and according to the conversation I had with @malfet about a year ago, PyTorch also hides the symbol of their own code, in addition to third party.)

Torchtext may want to do this in case client code imports the same package compiled differently.

## References:
- https://stackoverflow.com/a/22254251
- https://labjack.com/news/simple-cpp-symbol-visibility-demo

## Double conversion

```
nm torchtext/_torchtext.so| grep double_c | grep 'T __' | head -10
0000000000175a48 T __ZN17double_conversion13StrtodTrimmedENS_6VectorIKcEEi
000000000017175c T __ZN17double_conversion16PowersOfTenCache32GetCachedPowerForDecimalExponentEiPNS_5DiyFpEPi
00000000001716fc T __ZN17double_conversion16PowersOfTenCache36GetCachedPowerForBinaryExponentRangeEiiPNS_5DiyFpEPi
00000000001715bc T __ZN17double_conversion6Bignum11PlusCompareERKS0_S2_S2_
000000000016f7cc T __ZN17double_conversion6Bignum12AssignBignumERKS0_
000000000016f788 T __ZN17double_conversion6Bignum12AssignUInt16Et
000000000016f7a0 T __ZN17double_conversion6Bignum12AssignUInt64Ey
0000000000171188 T __ZN17double_conversion6Bignum13SubtractTimesERKS0_i
00000000001702e8 T __ZN17double_conversion6Bignum14SubtractBignumERKS0_
000000000016ff70 T __ZN17double_conversion6Bignum15AssignHexStringENS_6VectorIKcEE
```

## Sentencepiece

```
$ nm torchtext/_torchtext.so| grep sentencep | grep 'T __' | head -10
0000000000128718 T __ZN13sentencepiece10ModelProto12InternalSwapEPS0_
00000000001277fc T __ZN13sentencepiece10ModelProto14_InternalParseEPKcPN6google8protobuf8internal12ParseContextE
000000000012765c T __ZN13sentencepiece10ModelProto16default_instanceEv
0000000000128334 T __ZN13sentencepiece10ModelProto21CheckTypeAndMergeFromERKN6google8protobuf11MessageLiteE
00000000001276a0 T __ZN13sentencepiece10ModelProto5ClearEv
0000000000128620 T __ZN13sentencepiece10ModelProto8CopyFromERKS0_
0000000000127650 T __ZN13sentencepiece10ModelProto9ArenaDtorEPv
0000000000128338 T __ZN13sentencepiece10ModelProto9MergeFromERKS0_
0000000000127168 T __ZN13sentencepiece10ModelProto9_Internal12trainer_specEPKS0_
0000000000127178 T __ZN13sentencepiece10ModelProto9_Internal14self_test_dataEPKS0_
```




Discussion:

Resolving patch:
diff --git a/build_tools/setup_helpers/extension.py b/build_tools/setup_helpers/extension.py
--- a/build_tools/setup_helpers/extension.py
+++ b/build_tools/setup_helpers/extension.py
@@ -29,7 +29,7 @@ def _get_eca(debug):
         if platform.system() == "Windows":
             eca += ['-O2']
         else:
-            eca += ["-O3"]
+            eca += ["-O3", "-fvisibility=hidden"]
     return eca
 
 
@@ -114,6 +114,8 @@ def _build_third_party(debug):
             '-DCMAKE_EXPORT_COMPILE_COMMANDS=ON',
             f'-DCMAKE_INSTALL_PREFIX={_TP_INSTALL_DIR}',
             f'-DCMAKE_BUILD_TYPE={config}',
+            '-DCMAKE_CXX_VISIBILITY_PRESET=hidden',
+            '-DCMAKE_POLICY_DEFAULT_CMP0063=NEW',
         ] + extra_args + ['..'],
         cwd=str(build_dir),
         check=True,
@@ -144,8 +146,11 @@ def _build_sentence_piece(debug):
         extra_args = []
     subprocess.run(
         args=['cmake', '-DSPM_ENABLE_SHARED=OFF', f'-DCMAKE_INSTALL_PREFIX={_TP_INSTALL_DIR}',
+              '-DCMAKE_CXX_VISIBILITY_PRESET=hidden',
               '-DCMAKE_CXX_FLAGS=' + _get_cxx11_abi(),
+              '-DCMAKE_POLICY_DEFAULT_CMP0063=NEW',
               f'-DCMAKE_BUILD_TYPE={config}'] + extra_args + ['..'],
+
         cwd=str(build_dir),
         check=True,
         env=build_env,


Test patch: